response = httpclient.execute(httppost); 无反应解决方法

   @Override
                public void run()
                {
                        // 请求的地址
                        String url = "http://192.168.1.1/MyService/loginAction.action";
                        Log.d("me", "请求服务器进行登录验证的URL:" + url);

                        // 创建一个HttpClient对象
                        HttpClient httpclient = new DefaultHttpClient();

                        // 创建HttpPost
                        HttpPost httppost = null;
                        httppost = new HttpPost(url);

                        // 添加请求参数
                        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
                        formparams.add(new BasicNameValuePair("username", username));
                        formparams.add(new BasicNameValuePair("password", password));

                        UrlEncodedFormEntity uefEntity;

                        try
                        {
                                uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");

                                httppost.setEntity(uefEntity);

                                HttpResponse response;
                                // 执行Post请求
                                response = httpclient.execute(httppost);   //到这里卡住了,不向下执行了。但不抛出任何错误,也没有被catch捕获

                                // 如果请求成功
                                if (response.getStatusLine().getStatusCode() == 200)
                                {
                                        // 获取响应实体
                                        HttpEntity entity = response.getEntity();

                                        if (entity != null)
                                        {
                                                String json = EntityUtils.toString(entity, "UTF-8");
                                                Log.d("me", "从服务器返回的登录验证结果对应的json为:" + json);

                                                // 将json通过Message发送
                                                Message msg = handler.obtainMessage();
                                                msg.obj = json;
                                                handler.sendMessage(msg);
                                        }
                                }
                                else
                                {
                                        Log.d("me", "请求服务器失败,错误代码为:" + response.getStatusLine().getStatusCode());
                                }
                        }
                        catch (ClientProtocolException e)
                        {
                                e.printStackTrace();
                        }
                        catch (UnsupportedEncodingException e)
                        {
                                e.printStackTrace();
                        }
                        catch (IOException e)
                        {
                                e.printStackTrace();
                        }
                }
        }

 

自己的:

public  void ewayAddCard(String nameCard,String cardNub,String mm,String yy,String cvn) {

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("https://api.sandbox.ewaypayments.com/AccessCodes");
        try {
            httpPost.addHeader("Content-Type","application/json");
//            httpPost.addHeader("Authorization","Basic Rjk4MDJDUEY4Mm5TNy9tN1N4UFRQK21MY1U3MGRiNTNZTDBYNjBWa25UVmtPVEJjN2Z5WGVaWlA5WkZqRWpjNzZDOEdGUDpTaGFyZVBhc3M1NTY2");

            if (AccessCode != null&&FormActionURL!=null) {

                String accesscode = AccessCode;
                String fromAction = FormActionURL;
                System.out.println("------------accesscode-----------"+accesscode);
                System.out.println("-----------FormActionURL------------"+FormActionURL);

                HttpPost httpPost2 = new HttpPost(fromAction);

                List<NameValuePair> data = new ArrayList<NameValuePair>();
                data.add(new BasicNameValuePair("EWAY_ACCESSCODE", accesscode));
                data.add(new BasicNameValuePair("EWAY_PAYMENTTYPE", "Credit Card"));
                data.add(new BasicNameValuePair("EWAY_CARDNAME", nameCard));
                data.add(new BasicNameValuePair("EWAY_CARDNUMBER", cardNub));
                data.add(new BasicNameValuePair("EWAY_CARDEXPIRYMONTH", mm));
                data.add(new BasicNameValuePair("EWAY_CARDEXPIRYYEAR", yy));
                data.add(new BasicNameValuePair("EWAY_CARDCVN", cvn));


                httpPost2.setEntity(new UrlEncodedFormEntity(data,"UTF-8"));
                HttpResponse httpResponse2 = httpClient.execute(httpPost2);
                HttpEntity entity2 = httpResponse2.getEntity();
                if (null != entity2) {
                    String responseContent = EntityUtils.toString(entity2, "UTF-8");
                    System.out.println("-----------------------------"+responseContent);
                    if ("Credit Card bind failed".equals(responseContent)){
                        showToastInThread(getResources().getString(R.string.creditCard_fail));
                        this.finish();
                    }
                    else if ("Credit card binding is successful".equals(responseContent)){
                        showToastInThread(getResources().getString(R.string.creditCard_successful));
                        dialogs.dismiss();
                        this.finish();
                    }
                }else {
                    showToastInThread(getResources().getString(R.string.creditCard_fail));
                    dialogs.dismiss();
                    this.finish();
                }
            }
        } catch (Exception e) {
            showToastInThread(getResources().getString(R.string.creditCard_fail));
            e.printStackTrace();
            dialogs.dismiss();
            this.finish();
        } finally{
            httpClient.getConnectionManager().shutdown();
        }
    }

 

我的解决方法是这样的,不知道你们是否适用:

在onCreate方法里面加上这段:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads()
        .detectDiskWrites()
        .detectNetwork()   // or .detectAll() for all detectable problems
        .penaltyLog()
        .build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectLeakedSqlLiteObjects()
        .detectLeakedClosableObjects()
        .penaltyLog()
        .penaltyDeath()
        .build());

   原因是android阻止了post请求。networkonmain 的exception

转载于:https://my.oschina.net/c99k3/blog/783956

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值