0

reader.readLine()からデータを取得しました。しかし、whileループには入りません。リターン 'json'文字列はnullです。Android HttpurlConnectionはBufferedReaderを使用してデータを読み取りましたが、StringBuilderに追加した後にnull値を取得しました

誰かが問題を解決するのを手伝ってください。私はすでに正しい解決策を試してみました。

try {    
    URL urL = new URL(url); 
    urlConnection = (HttpURLConnection) urL.openConnection(); 
    urlConnection.setRequestMethod("POST"); 
    urlConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    urlConnection.addRequestProperty("Accept", "application/json"); 
    urlConnection.setConnectTimeout(10000); 
    urlConnection.setReadTimeout(15000); 
    urlConnection.setDoInput(true); 
    urlConnection.setDoOutput(true); 
    Uri.Builder builder = new Uri.Builder() 
      .appendQueryParameter("PackageName", packagename); 

    String query = builder.build().getEncodedQuery(); 

    OutputStream os = urlConnection.getOutputStream(); 
    BufferedWriter writer = new BufferedWriter(
      new OutputStreamWriter(os, "UTF-8")); 
    writer.write(query); 
    writer.flush(); 
    writer.close(); 
    os.close(); 

    urlConnection.connect(); 

    InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 

    BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
    StringBuffer result = new StringBuffer(); 

    Log.e("READER","....."+reader.readLine()); 
    String line=reader.readLine(); 
    while (line != null) { 
     result.append(line).append('\n'); 
    } 
    json=result.toString(); 
    in.close(); 

} catch (Exception e) { 
    e.printStackTrace(); 
} finally { 
    urlConnection.disconnect(); 
} 
Log.e("RESULT","...."+json); 
return json; 
+0

私はあなたのコードは 'だけで、コードの上に見える___' {試みる含んだと思いますが、共有スニペット___ –

+1

catch文の前にtryブロックが閉じられていません –

+1

Atlast私はそれを取得しました。問題は行Log.e( "READER"、 "....." + reader.readLine())にありました。 文字列= reader.readLine(); 私は上記の行を と変更しました。String line = reader.readLine(); Log.e( "READER"、 "....." +行); 今すぐ動作します... サポートに感謝します。:) –

答えて

0

Log.e("READER","....." + reader.readLine()); 

このコードを試してみてください、ログ内の余分なreader.readLine()を外し、

Log.e("ResponseCode", "" + urlConnection.getResponseCode()); 

InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
StringBuffer result = new StringBuffer(); 

String line; 
while ((line = reader.readLine()) != null) { 
    result.append(line); 
} 
json = result.toString(); 
in.close(); 
+0

提案をありがとうございます。私はすでに同じことをしています。しかし、それと同じ問題です。 ( –

+0

)APIからデータを取得していることを確認してください。 –

+0

はい、データはAPI –

0

は、これは基本だ多分あなたはそれを逃してください:

json=result.toString(); 
    in.close(); 

} catch (Exception e) { 
    e.printStackTrace(); 
} finally { 
    urlConnection.disconnect(); 
} 
Log.e("RESULT","...."+json); 
if (json!=null) 
return json; 

それをnull pをスキップしますointer例外:あなたがやっているすべてのAPIからjsonStringをダウンロードしている場合、これはあなたを

+0

いいえ、それは私の問題を解決していません –

0

整理する必要があります。

String jsonString = ""; 

    try { 

     URL mURL = new URL(yourURL); 

     HttpURLConnection mHttpURLConnection; 
     mHttpURLConnection = (HttpURLConnection)mURL.openConnection(); 

     mHttpURLConnection.setDoInput(true); 

     mHttpURLConnection.connect(); 

     InputStream mInputStream = mHttpURLConnection.getInputStream(); 

     int i = 0; 
     while ((i = mInputStream.read()) != -1) { 
      jsonString += (char) i; 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 

    } catch (IOException e) { 
     e.printStackTrace(); 

    } 
Log.e("JSON","Json String received: " + jsonString); 
関連する問題