我已经尝试了几个小时向 php 发送 POST 请求,该 php 发送回 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;
}