スクリプトを使用してログインアクティビティを実装しようとしています。スクリプトは機能しており、私はすでに郵便配達のアプリケーションでスクリプトをテストしています。私は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;
}
あなただけの次のプロパティを追加する必要があるかもしれません: 'httpURLConnection.setRequestProperty( "Content-Type"、 "application/json; charset = UTF-8"); ' – Barns
正確に入力してください。ここでは、常に「入力ストリームがヌル」または「レスポンス...常にヌルです」はありません。何があるかは* empty *レスポンスです。あなたのコード*によってnullに変換されます。あるいは、あなたがここで報告しなかった 'IOException'を返します。その後、あなたのコードはnullを返します。単なる 'return'ステートメントのまわりの' try/catch'は全く無意味です。 – EJP