response=httpClient.execute(request) 一直给出 NetworkOnMainThreadException
response=httpClient.execute(request) keeps giving NetworkOnMainThreadException
几个小时以来,我一直在尝试向 php 发送 POST 请求,后者发回 json 响应。但是那一行代码一直给我带来问题,我似乎无法前进。我很沮丧。我已经查阅了无数其他人使用同一条线的例子!但出于某种原因,它对我不起作用!
这是我的 doinBackgrounds 代码片段:
protected Void doInBackground(Void... voids) {
HttpClient httpClient=new DefaultHttpClient();
HttpGet request=new HttpGet();
BufferedReader in=null;
String data=null;
HttpResponse response = null;
try {
URI website=new URI("login.php");
request.setURI(website);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpPost httpPost=new HttpPost("login.php");
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("password", pwd));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
response=httpClient.execute(request);
} catch (IOException e) {
e.printStackTrace();
}
try {
in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (IOException e) {
e.printStackTrace();
}
try {
String line=in.readLine();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
if (Utility.isNotNull(name) && Utility.isNotNull(pwd)) {
RequestParams params = new RequestParams();
if (Utility.validate(name, pwd)) {
params.put("username", name);
params.put("password", pwd);
bloggedIn=true;
onPostExecute();
} else {
loginActivity.InvalidToast();
}
} else {
loginActivity.EmptyToast();
}
return null;
}
您正在给 doInBackground()
打电话。这不是您使用 AsyncTask
的方式。要执行 AsyncTask
,请创建一个实例并对其调用 execute()
或 executeOnExecutor()
。这将导致 doInBackground()
在后台线程上变为 运行。
您可以在 the JavaDocs 中阅读有关 AsyncTask
的更多信息。
(顺便说一句,以后请 post 堆栈跟踪作为从 LogCat 复制的文本,而不是屏幕截图)
protected Void doInBackground(Void... voids) {
HttpClient httpClient=new DefaultHttpClient();
HttpGet request=new HttpGet();
BufferedReader in=null;
String data=null;
HttpResponse response = null;
try {
URI website=new URI("login.php");
request.setURI(website);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpPost httpPost=new HttpPost("login.php");
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("password", pwd));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
response=httpClient.execute(request);
} catch (IOException e) {
e.printStackTrace();
}
try {
in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (IOException e) {
e.printStackTrace();
}
try {
String line=in.readLine();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
if (Utility.isNotNull(name) && Utility.isNotNull(pwd)) {
RequestParams params = new RequestParams();
if (Utility.validate(name, pwd)) {
params.put("username", name);
params.put("password", pwd);
bloggedIn=true;
onPostExecute();
} else {
loginActivity.InvalidToast();
}
} else {
loginActivity.EmptyToast();
}
return null;
}
您正在给 doInBackground()
打电话。这不是您使用 AsyncTask
的方式。要执行 AsyncTask
,请创建一个实例并对其调用 execute()
或 executeOnExecutor()
。这将导致 doInBackground()
在后台线程上变为 运行。
您可以在 the JavaDocs 中阅读有关 AsyncTask
的更多信息。
(顺便说一句,以后请 post 堆栈跟踪作为从 LogCat 复制的文本,而不是屏幕截图)