2017-07-22 24 views
-2

getInputStream()がnullを返す。私はそれを探しましたが、何の助けも得られませんでした。誰もそれを解決する方法を教えてくださいできますか?HttpURLConnection getInputStreamがnullを返す

HttpURLConnection urlConnection = null; 
InputStream inputStream = null; 
try { 
    urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setReadTimeout(100000 /* milliseconds */); 
    urlConnection.setConnectTimeout(150000 /* milliseconds */); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection.connect(); 

    // If the request was successful (response code 200),`` 
    // then read the input stream and parse the response. 
    if (urlConnection.getResponseCode() == 200) { 
     inputStream = urlConnection.getInputStream(); 
     jsonResponse = readFromStream(inputStream); 
    } else { 
    // Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode()); 
    } 
} 
+1

完全なエラースタックトレースを確認できますか? –

答えて

1

私のコードを使用してください。

String response; 

HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setReadTimeout(10000); 
connection.setConnectTimeout(10000); 
connection.setRequestMethod("GET"); 
connection.setUseCaches(false); 
connection.setAllowUserInteraction(false); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
int responceCode = connection.getResponseCode(); 

if (responceCode == HttpURLConnection.HTTP_OK) 
{ 
    String line; 
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
    while ((line = br.readLine()) != null) 
    { 
      response = "";// String variable declared global 
      response += line; 
      Log.i("response_line", response); 
    } 
} 
else 
{ 
     response = ""; 
} 
+0

getInputStream()はapiのURLに関連していますか? – zohaib

+0

これはPOSTです。 OPはGETをやりたい – EJP

+0

@EJPそのものGET ..チェックコードは正しく –