2017-06-27 10 views
-2

スクリプトを使用してログインアクティビティを実装しようとしています。スクリプトは機能しており、私はすでに郵便配達のアプリケーションでスクリプトをテストしています。私はjson形式でデータを送信しており、サーバーはjson形式で応答を送信します。応答を読み込もうとすると、常にnullです。誰かが助けてくれますか?常にnull入力ストリーム

@Override 
    protected String doInBackground(String... params) { 
     String data=""; 
     JSONObject jsonobj = new JSONObject(); 
     JSONObject Data=new JSONObject(); 
     try { 
      Data.put("service","emailsignin"); 
      Data.put("email_id",mEmail); 
      Data.put("password",mPassword); 
      Data.put("device_token","ABCD"); 
      Data.put("device_type","A"); 
      Data.put("user_type_id","2"); 
      jsonobj.put("data",Data); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     HttpURLConnection httpURLConnection = null; 
     BufferedReader reader=null; 
     String write_data=String.valueOf(jsonobj); 
     Log.e("write_data:",write_data); 
     try { 
      httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 

      httpURLConnection.setDoInput(true); 

      Writer writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8")); 
      writer.write(write_data); 
      writer.close(); 
      InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream()); 
      reader = new BufferedReader(newInputStreamReader(inputStream)); 
      StringBuilder buffer = new StringBuilder(); 

      String inputLine; 
      while ((inputLine = reader.readLine()) != null) { 
       Log.e("LoginActivity:",inputLine); 
       buffer.append(inputLine).append("\n"); 
      } 
      if (buffer.length() == 0) { 
       // Stream was empty. No point in parsing. 
       Log.e("LoginActivity:","input string empty"); 
       return null; 
      } 
      data = buffer.toString(); 

      Log.e("Response is:",data); 
      try { 

       return data; 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 


     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      if (httpURLConnection != null) { 
       httpURLConnection.disconnect(); 
      } 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (final IOException e) { 
        Log.e("Log In:", "Error closing stream", e); 
       } 
      } 
     } 
     return null; 


    } 
+0

あなただけの次のプロパティを追加する必要があるかもしれません: 'httpURLConnection.setRequestProperty( "Content-Type"、 "application/json; charset = UTF-8"); ' – Barns

+0

正確に入力してください。ここでは、常に「入力ストリームがヌル」または「レスポンス...常にヌルです」はありません。何があるかは* empty *レスポンスです。あなたのコード*によってnullに変換されます。あるいは、あなたがここで報告しなかった 'IOException'を返します。その後、あなたのコードはnullを返します。単なる 'return'ステートメントのまわりの' try/catch'は全く無意味です。 – EJP

答えて

-1

あなたはretrofitまたはloopjのようなあなたのためのサーバ要求を行ういくつかのライブラリを使用することができます。私はあなたの要求に間違っていると思います。またはあなたが(あなたがorg.apache.http jarファイルを必要とするかのGradleでそれを追加します)サーバーにJSONを投稿するために、このメソッドを使用することができます。

public static String POST(String url){ 
    InputStream inputStream = null; 
    String result = ""; 
    try { 

     // 1. create HttpClient 
     HttpClient httpclient = new DefaultHttpClient(); 

     // 2. make POST request to the given URL 
     HttpPost httpPost = new HttpPost(url); 

     String json = ""; 

     // 3. build jsonObject 
     JSONObject jsonObject = new JSONObject(); 
     //your json fields fill here 

     // 4. convert JSONObject to JSON to String 
     json = jsonObject.toString(); 

     // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
     // ObjectMapper mapper = new ObjectMapper(); 
     // json = mapper.writeValueAsString(person); 

     // 5. set json to StringEntity 
     StringEntity se = new StringEntity(json); 

     // 6. set httpPost Entity 
     httpPost.setEntity(se); 

     // 7. Set some headers to inform server about the type of the content 
     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 

     // 8. Execute POST request to the given URL 
     HttpResponse httpResponse = httpclient.execute(httpPost); 

     // 9. receive response as inputStream 
     inputStream = httpResponse.getEntity().getContent(); 

     // 10. convert inputstream to string 
     if(inputStream != null) 
      result = convertInputStreamToString(inputStream); 
     else 
      result = "Did not work!"; 

    } catch (Exception e) { 
     Log.d("InputStream", e.getLocalizedMessage()); 
    } 

    // 11. return result 
    return result; 
} 

private static String convertInputStreamToString(InputStream inputStream) throws IOException{ 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
    String line = ""; 
    String result = ""; 
    while((line = bufferedReader.readLine()) != null) 
     result += line; 

    inputStream.close(); 
    return result; 

} 
+0

助けてくれてありがとう。これは私のために働いた。 –

関連する問題