我是Android Web服务的新手,我想与php服务器通信,我的响应代码是200但是Buffered Reader在readline上返回null。
我不知道是什么问题。请帮助我
我正在使用:
执行AsyncTask任务new AsysnchronousTask().execute("");
以及AsyncTask的完整代码如下
public class AsysnchronousTask extends AsyncTask<String, String, String> {
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Log.d(" sever Resutl ", result);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result = "";
HttpClient mDefaultHttpClient = new DefaultHttpClient();
HttpPost mHttpPost = new HttpPost(PATH);
// JSONObject sendJsonObject= new JSONObject();
JSONObject postJsonObject = new JSONObject();
HttpResponse mResponse;
try {
try {
postJsonObject.put("email", "hotspot.st@gmail.com");
postJsonObject.put("login", "alan");
postJsonObject.put("password", "120519");
postJsonObject.put("language", "en");
JSONArray mJsonArray = new JSONArray();
mJsonArray.put(postJsonObject);
mHttpPost.setHeader("REGISTER", postJsonObject.toString());
mHttpPost.getParams().setParameter("jsonpost", mJsonArray);
// post the data
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mResponse = mDefaultHttpClient.execute(mHttpPost);
StatusLine statuscod = mResponse.getStatusLine();
int statuscode = statuscod.getStatusCode();
Header[] mHeader= mResponse.getAllHeaders();
if (statuscode == 200) {
HttpEntity mEntity = mResponse.getEntity();
if (mEntity != null) {
InputStream mInputStream = mEntity.getContent();
// Converting Stream into the string
BufferedReader mBufferedReader = new BufferedReader(
new InputStreamReader(mInputStream));
StringBuilder mBuilder = new StringBuilder();
String line = null;
while ((line = mBufferedReader.readLine()) != null) {
mBuilder.append(line + "/n");
}
mBufferedReader.close();
mInputStream.close();
}
else {
Log.d(TAG, "Fail to Read from Server ");
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
JSONArray mJsonArray = new JSONArray(result);
JSONObject mJsonObject = null;
for (int i = 0; i < mJsonArray.length(); i++) {
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
答案 0 :(得分:0)
我发现在尝试解决您的响应问题之前,您可以通过几种方法来改进代码。我建议您在发送信息时实现BasicNameValuePair ArrayList而不是JSON。在您的情况下,实现如下
enter code InputStream is = null;
String result = "";
String key = "";
ArrayList<SharePrices> results = new ArrayList<SharePrices>();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "hotspot.st@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("login", "alan"));
nameValuePairs.add(new BasicNameValuePair("password", "120519"));
nameValuePairs.add(new BasicNameValuePair("language", "en"));
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PATH);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in connection " + e.toString());
// results.setText("Error in connection");
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
} here