我正在從API響應,當我從郵差提交請求等所示的圖像中從Android的我想要調用與POST方法API
screenshot1.jpg =我需要通過 screenshot2.jpg數據=的結果我們得到
我試着打電話給他們通過與下面的代碼的Android,但它不工作,
JSONObject login = new JSONObject();
login.put("username", userName);
login.put("password", password);
login.put("platform", "ANDROID");
login.put("location", "56.1603092,10.2177147");
String str = WebServices.excutePost(url, login);
public static String excutePost(String targetURL, JSONObject urlParameters) {
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/raw");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.toString().getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(String.valueOf(urlParameters.toString().getBytes("UTF-8")));
out.close();
//connection.disconnect();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
你能說說'不行'嗎?它在做什麼或不做什麼?是否有錯誤訊息? – Breavyn
我認爲你的內容類型應該是應用程序/ json –
如果可能,你可以提供堆棧跟蹤問題嗎? –