将数据发送到服务器并处理响应-Android
我正在尝试将数据发布到Web服务,但似乎无法正确执行此操作!所以我试图将json发布到服务器上,但我不知道怎么做。我需要发送此示例json以获得json响应: 内容类型:application/json HTTPMethod:POST HTTPBody: {CouponVerificationCode:594952223490,ApiKey:zfywqdyukxqpvg86snpd1osslr7q6dgegbq1f7p2yetxb56y,令牌:_2_jx1yfvtzgglntjbodw3gdzmnnagptwztwzt7dc6grnaikhx9pwv75b776gqlzou 2_SxMJjq8_2_kadmyx59hczoyaw=} 但是我得到的不是json响应,而是html响应。 有人能帮我解决这个问题吗 这是im用于与服务器通信的代码:将数据发送到服务器并处理响应-Android,android,json,web-services,android-json,android-webservice,Android,Json,Web Services,Android Json,Android Webservice,我正在尝试将数据发布到Web服务,但似乎无法正确执行此操作!所以我试图将json发布到服务器上,但我不知道怎么做。我需要发送此示例json以获得json响应: 内容类型:application/json HTTPMethod:POST HTTPBody: {CouponVerificationCode:594952223490,ApiKey:zfywqdyukxqpvg86snpd1osslr7q6dgegbq1f7p2yetxb56y,令牌:_2_jx1yfvtzgglntjbodw3gdzm
public static String makeRequest(String path) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(path);
JSONObject holder = new JSONObject();
// holder.accumulate(name, value)
String test = "{\"ApiKey\":\"somekey\","
+ "\"OperativeSystem\":\"0\","
+ "\"AppVersion\":\"1\","
+ "\"WebServiceVersion\":\"1\"}";
StringEntity se = new StringEntity(test);
Log.v("--", test);
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpost, responseHandler);
// HttpEntity entity = response.getEntity();
Log.v("--", response);
return response;
}
尝试使用httpGet而不是httpPost。这是我的工作代码:
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpGet httpGet =new HttpGet(url);
String User = context.getString(R.string.json_uname);
String Password = context.getString(R.string.json_pwd);
httpGet.setHeader("Authorization", "Basic "+ new String(Base64.encode((User +":"+ Password).getBytes(),Base64.NO_WRAP )));
HttpResponse httpResponse = httpclient.execute(httpGet);
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
{
//convertInputStreamToString(inputStream);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String json = reader.readLine();
JSONTokener tokener = new JSONTokener(json);
finalResult = new JSONArray(tokener);
}
else
finalResult =null;
您需要解析响应并将其转换为string@geekCode响应是html而不是JSON您现在得到的答案是什么?@DarkoPetkovski-您需要确认以下组合url、请求类型、请求内容类型。如果其中一个不正确,您将无法正确访问Web服务。而且也没有发布任何数据。。。