2013-10-09 47 views
1

我是Android Web服务的新手,我想与php服务器通信,我的响应代码是200,但是Buffered Reader在readline上返回null。Android JSON响应

我不知道什么是问题。好心帮我

我执行的AsyncTask任务使用:针对的AsyncTask

new AsysnchronousTask().execute(""); 

,完整的代码如下

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", "[email protected]"); 
        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

http://android-spirit.blogspot.in/2013/08/cosume-phppost-method-webservice-in.html – Nirmal

+1

谢谢尼日利亚..好的链接 – kami

+0

欢迎开心编码.... – Nirmal

回答

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", "[email protected]")); 
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 
+0

ArrayList results = new ArrayList (); ? – kami

+0

对不起,这是我自己的类实现打包响应,但既然你不需要它,你可以忽略它 –

+0

感谢您的帮助,我测试了这个,reader.readline()为null,没有任何回报,与以前相同的问题码。 – kami