0
doInBackgroundメソッド内のAsync TaskでHttpUrlConnectionクラスを使用しようとしています。HttpUrlConnection内でPOSTメソッドを使用する方法
@Override
protected String doInBackground(String... params) {
String strUrl=String.format("%s",SERVER_URL);
try {
URL url =new URL(strUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Content-Type","application/json");
String str="{\"userid\":\"1\"}";
byte[] outputInBytes = str.getBytes("UTF-8");
OutputStream os = urlConnection.getOutputStream();
os.write(outputInBytes);
os.close();
InputStream stream1=urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(stream1);
StringBuilder builder = new StringBuilder();
int chr;
while((chr=reader.read())!=-1)
{
builder.append((char)chr);
}
String result=builder.toString();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
しかし、私は結果の代わりにnullを取得していますが、ここで問題になるでしょう。私たちのURLは、特定のパケットにチャンクを取得この方法により
- :
コードをデバッグしてコードフローを確認してください –
これを取得と送信の両方に使用できますhttp://stackoverflow.com/questions/37289109/calling-a-async-task-and-waiting-for-answer? noredirect = 1#comment62106624_37289109 –
あなたはサーバーコードまたは結果でnullを取得していますか?、結果の値はサーバーが応答するものになります。 –