-3

在我的一个 android 应用程序中,我在调用 API 期间收到错误“java.net.ProtocolException:已建立连接”。(我在 HttpsURLConnection 中引用了“已建立连接”异常以获取解决方案,但没有用)

下面是我调用 API 的代码。代码适用于除一个之外的所有 API。该 API 的开发方式与其他 API 相同,但仍低于该 API 的代码抛出错误。

HttpURLConnection con = null;
                BufferedReader in = null;
                StringBuilder sb = new StringBuilder();
                try {

                    URL uri = null;
                    uri = new URL(url);
                    LogUtil.v("~~URL - "+url);

                    con = (HttpURLConnection) uri.openConnection();
                    System.setProperty("http.keepAlive", "false");
                    con.setRequestMethod(requestType); //type: POST, PUT, DELETE, GET
                    con.setDoOutput(true);
//                  con.setDoInput(true);
                    con.setConnectTimeout(TIMEOUT); //20 secs
                    con.setReadTimeout(TIMEOUT); //20 secs
                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                    String authorizationString = "Basic " + Base64.encodeToString((myAuthString).getBytes(), Base64.DEFAULT); 

                    con.setRequestProperty("Authorization", authorizationString);

                    if( body != null){

                        LogUtil.v("~~BODY - "+body);

                        DataOutputStream out = new  DataOutputStream(con.getOutputStream());
                        out.writeBytes(body);
                        out.flush();
                        out.close();
                    }

                    con.connect();
                    InputStream inst = con.getInputStream();
                    in = new BufferedReader(new InputStreamReader(inst));

                    String temp = null;

                    while((temp = in.readLine()) != null){
                        sb.append(temp).append(" ");
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    if(in!=null){
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(cb!=null){
                        if (sb.toString().length() >0) {
                            LogUtil.v("~~ RESPONSE - "+ sb.toString());
                            cb.onResponse(sb.toString(), actionCode);
                        }else{
                            LogUtil.v("~~ RESPONSE - null");
                            cb.onResponse(null, actionCode);
                        }
                    }
                                    if(con!=null)
                                            con.disconnect();

                }

请不要建议在这里使用 DefaultHttpClient。

我想使用上述方法解决问题,因为它适用于所有其他 API。

仅供参考:API 调用已使用浏览器检查,它正在返回返回数据。从安卓调用时只是抛出错误。

~~~~~~ 已编辑 ~~~~~~~~~ (服务器端 API 是用 PHP 开发的)

让我简化我的问题解释:

function API1Call(){
   // callAPI(param...)
}

function API2Call(){
   // callAPI(param...)
}

callAPI(param..){

// above API call code goes here
}

如果您看到函数 callAPI(param...),它会在需要调用 API 时执行。

***> 现在在我的项目中,我调用了许多写在不同活动上的 api。

仅适用于一个 API 调用,上面的 callAPI(param...) 抛出我上面提到的异常。***

~~~~~我的日志猫~~~~~~~

08-31 14:22:45.309: W/System.err(27364): java.net.ProtocolException: Connection already established
08-31 14:22:45.314: W/System.err(27364):    at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:666)
08-31 14:22:45.324: W/System.err(27364):    at libcore.net.http.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:144)
08-31 14:22:45.329: W/System.err(27364):    at 
com.indapoint.ewe.api.EWeAPI$1.run(EWeAPI.java:245)
08-31 14:22:45.334: W/System.err(27364):    at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

3

不要con.connect()getOutputStream()通话后使用,因为第二个无论如何都会调用 connect() 。

于 2013-08-31T06:47:59.607 回答