POST
を使用して文字列をurl
に送信する必要があります。 文字列は次のようになります。POSTを使用してHttpURLConnectionを使用してjsonデータを送信する
data={"cmd":"sign_in",....}
のでdoInBackground
に、私はこのコードを使用:
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String json = "{\"cmd\": \"sign_in\",...";
try{
URL url = new URL("https://...?data=");
connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("charset", "utf-8");
connection.connect();
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write(json);
osw.flush();
osw.close();
}
私は
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
if(serverResponseMessage.length()>0)
return true;
を使用して、私の応答をチェックしかし、私は何の応答も受信していません。応答文字列はJSON
文字列でなければなりません。しかし、私はlength()
を使用して、文字列が返されているかどうかを確認しました。 このコードは動作しますか?どんな助け?
を試してみてください? – Piyush
アンドロイド外で試したときにAPIが動作していますか? –
Neeraj、APIが動作しています。 – Amit