android httpurlconnection 参数,带有发帖请求和参数的HttpUrlConnection作为Json对象android...

嗨,我正在开发一个小型android应用程序,在其中我想将HttpUrlConnection发布请求与params作为json对象一起使用.但是它对我不起作用,我通过以下方式做到了:

try

{

URL url;

DataOutputStream printout;

DataInputStream input;

url = new URL ("https://abc.com");

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setRequestMethod("POST");

urlConnection.setDoInput (true);

urlConnection.setDoOutput (true);

urlConnection.setUseCaches (false);

urlConnection.setConnectTimeout(10000);

urlConnection.setReadTimeout(10000);

urlConnection.setRequestProperty("Content-Type","application/json");

urlConnection.connect();

JSONObject jsonParam = new JSONObject();

JSONArray arr = new JSONArray();

arr.put("LNCf206KYa5b");

arr.put("oWdC0hnm1jjJ");

jsonParam.put("places", arr);

jsonParam.put("action", "Do");

printout = new DataOutputStream(urlConnection.getOutputStream ());

printout.writeUTF(URLEncoder.encode(jsonParam.toString(),"UTF-8"));

printout.flush ();

printout.close ();

int HttpResult =urlConnection.getResponseCode();

if(HttpResult ==HttpURLConnection.HTTP_OK){

BufferedReader br = new BufferedReader(new InputStreamReader(

urlConnection.getInputStream(),"utf-8"));

String line = null;

while ((line = br.readLine()) != null) {

sb.append(line + "\n");

}

br.close();

//System.out.println(""+sb.toString());

}else{

System.out.println(urlConnection.getResponseMessage());

}

} catch (MalformedURLException e) {

e.printStackTrace();

}

catch (IOException e) {

e.printStackTrace();

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(urlConnection!=null)

urlConnection.disconnect();

}

}

它没有给出任何响应代码或任何输出.难道我做错了什么.如何解决这个问题.需要帮忙.谢谢 .

我收到以下系统错误

06-07 09:55:58.171: W/System.err(4624): java.io.IOException: Received authentication challenge is null

06-07 09:55:58.171: W/System.err(4624): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:1153)

06-07 09:55:58.171: W/System.err(4624): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:1095)

06-07 09:55:58.171: W/System.err(4624): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1048)

06-07 09:55:58.171: W/System.err(4624): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)

06-07 09:55:58.179: W/System.err(4624): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:121)

06-07 09:55:58.179: W/System.err(4624): at com.mobiotics.qcampaigns.data.operation.ProximityOperation.execute(ProximityOperation.java:187)

06-07 09:55:58.179: W/System.err(4624): at com.foxykeep.datadroid.service.RequestService.onHandleIntent(RequestService.java:145)

06-07 09:55:58.179: W/System.err(4624): at com.foxykeep.datadroid.service.MultiThreadedIntentService$IntentRunnable.run(MultiThreadedIntentService.java:170)

解决方法:

由于401响应码而发生此错误.

您可以像这样检查第一个响应代码

int responsecode = response.getStatusLine().getStatusCode();

如果服务器回复401,则抛出此异常.

401的实际原因是您没有在此时发送OAuth验证程序代码.

你可以参考下面的代码

String url = LoginUrl;

String resultstring = "";

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(url);

// Add your data

List nameValuePairs = new ArrayList(5);

nameValuePairs.add(new BasicNameValuePair("j_username", username));

nameValuePairs.add(new BasicNameValuePair("j_password", password));

try {

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpContext localContext = new BasicHttpContext();

localContext.setAttribute(ClientContext.COOKIE_STORE,

Util.cookieStore);

try {

HttpResponse response = httpclient.execute(httppost,

localContext);

int responsecode = response.getStatusLine().getStatusCode();

if (responsecode == 200) {

Util.responsecode = responsecode;

resultstring = "Success";

InputStream in = response.getEntity().getContent();

resultstring = Util.convertinputStreamToString(in);

} else if (responsecode == 401) {

Util.responsecode = responsecode;

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

标签:android,post,parameters,httpurlconnection

来源: https://codeday.me/bug/20191011/1889724.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值