我正在开发一个项目,用Java servlet传递Android应用程序。在这个android应用程序中,在editText视图中输入的字符串应作为请求发送到Java servlet,Java servlet应将此字符串替换为另一个字符串作为响应。
以下是我的Android应用代码:
public class DoubleMeActivity extends Activity implements OnClickListener {
static EditText inputValue=null;
Integer doubledValue =0;
Button doubleMe;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate);
inputValue = (EditText) findViewById(R.id.inputNum);
doubleMe = (Button) findViewById(R.id.doubleme);
tv = (TextView) findViewById(R.id.textView2);
doubleMe.setOnClickListener(this);
// check if you are connected or not
if(isConnected()){
tv.setBackgroundColor(0xFF00CC00);
tv.setText("You are conncted");
}
else{
tv.setText("You are NOT conncted");
}
}
@Override
public void onClick(final View v) {
switch (v.getId()){
case R.id.doubleme:
if(!validate())
Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
else
new HttpAsyncTask().execute("http://139.179.92.123:8080/SmartStoreServlet/FemtoStore");
break;
}
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
public static String POST(String url){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("string" , inputValue.getText().toString());
String json = jsonObject.toString();
System.out.println(json);
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
System.out.println("ajsajsd" + inputStream); --> is not executed
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
{
result = convertInputStreamToString(inputStream);
System.out.println("jndkjsn" + result);
}
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
private boolean validate(){
if(inputValue.getText().toString().trim().equals("")) //DO CHECK
return false;
else
return true;
}
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
和我的java servlet代码:
public class FemtoStore extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FemtoStore() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(request.getRemoteAddr());
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if(br != null){
json = br.readLine();
}
System.out.println(json);
}
}
问题是我在Android应用程序端获得连接拒绝错误,而我正在使用真实设备。如果你帮助我,我将非常感激。