我的应用程序在到达线路时崩溃:
HttpEntity entity = response.getEntity();
我不知道为什么,所以我尝试在调试模式下运行,但 Android Studio 一直告诉我:
警告:调试信息可能不可用。请关闭其他使用 ADB 的应用程序:Monitor、DDMS、Eclipse
而且我没有打开任何其他东西,实际上我只是重置了PC。
这是我的整个代码:
package com.clientserver.jsontest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
/**
* Created by Gabriel on 25/08/13.
*/
public class SyncActivity extends Activity {
private TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sync_activity);
initComponents();
String name = "Gabriel";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nameToSearch", name));
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost("http://10.0.0.6/jsonTest/login.php");
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = response.getEntity(); //ERROR ON THIS LINE
String result = "Nothing";
try {
result = EntityUtils.toString(entity);
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
int numint = 0;
String clientName = "";
String email = "";
double amount = 0;
try {
numint = jsonObject.getInt("numint");
clientName = jsonObject.getString("name");
email = jsonObject.getString("email");
amount = jsonObject.getDouble("amount");
} catch (JSONException e) {
e.printStackTrace();
}
String text = numint + " "+ clientName + " " + email + " " + amount;
display.setText(text);
}
public void initComponents() {
display = (TextView) findViewById(R.id.textView);
}
}