我一直在尝试将一个POST请求发送回发送回json响应的php。但是这一行代码一直给我带来问题,我似乎无法前进。我很沮丧。我查阅了无数其他使用相同线路的例子!但由于某种原因,它不会为我工作! 这是我的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;
}
答案 0 :(得分:0)
您自己致电doInBackground()
。这不是您使用AsyncTask
的方式。要执行AsyncTask
,请创建一个实例并在其上调用execute()
或executeOnExecutor()
。这将导致doInBackground()
在后台线程上运行。
您可以在the JavaDocs中详细了解AsyncTask
。
(顺便说一句,将来,请将堆栈跟踪发布为从LogCat复制的文本,而不是截图)