passing parameter from android to servlet/jsp
i m not able to get the value that is pass from android in my jsp page. where i made make mistake. Any suggestion!
below is my code:
==========================
Client : android Activity
package org.test2;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class test2Activity extends Activity {
EditText ed;
Button ok;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed=(EditText)findViewById(R.id.EditText01);
ok=(Button)findViewById(R.id.Button01);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Create a new HttpClient and Post Header开发者_开发百科
HttpClient httpclient = new DefaultHttpClient();
HttpGet request=new HttpGet("http://localhost:8080/t5/jsp1.jsp?name=hiren");
//HttpPost httppost = new HttpPost("http://localhost:8080/t5/jsp1.jsp?name=hiren");
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(request);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
}catch (IOException e) {
// TODO Auto-generated catch block
}
}
});
}
}
=======================================
server : jsp
<%@ page import="java.util.*" language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Parameter value =
<%=request.getParameter("name") %>
</body>
</html>
=========================================
Two major problems here according to me:
You are using localhost and I suppose you are trying to use Emulator so instead of localhost you have to assign IP of your machine and if suppose you have DHCP then in Android localhost should be done in this way So try
"http://10.0.2.2:8080/myapp/servletname?key=value"
You are trying to print in browser instead my suggestion is try to print the value in Console it will show you and then you can save it to DB or do the process.
Hope this may solve the problem or any developer facing such a problem.
精彩评论