2017-05-04 3230 views
1

我一直在嘗試使用具有JSON響應的GET方法來訪問webservice。通過android但拋出連接拒絕異常。我能夠在瀏覽器上打開URL並獲得響應,但獲取響應需要大約30秒的時間。當我運行我的應用程序時,它等待響應並拋出連接拒絕異常。請幫忙。HttpHostConnectException:連接被拒絕

Login.java(其由OnClick方法的)

class Login{ 

login.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       pro.setVisibility(View.VISIBLE); 
       new ExecuteTask().execute(); 
       /* do{ 
        pro.setVisibility(View.VISIBLE); 
       }while (response=resp())*/ 




       /* if(response.contains("name")){ 

        startActivity(new Intent(Login.this,Tab.class)); 
        overridePendingTransition(R.anim.enter, R.anim.exit); 
       } 
       else if(response.contains("errorMessages")){ 
        showPopUp(); 

       } 
       else if(response.contains("<html>")||response.contains("site can't be reached")){ 
        showerrorPopUp(); 
       } 
       else{ 
        showerrorPopUp(); 

       }*/ 

      } 

     }); 

異步任務 它調用RESP()方法

public class ExecuteTask extends AsyncTask<String,Integer,String>{ 


    @Override 
    protected String doInBackground(String... params) { 
     System.out.println("into async task"); 
     String res=resp(); 
     System.out.println("into execute task"+ res); 
     return res; 

    } 
    @Override 
    protected void onPostExecute(String result) { 
     pro.setVisibility(View.GONE); 
     //progess_msz.setVisibility(View.GONE); 

    } 
} 

RESP()方法: 這個我thod由Http連接代碼組成 public String resp(){0} 0 0 0 0 0 0 0 0 0 0 0 0 0 System.out.println(「resp strt」);

 System.out.println("into resp"); 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(append(URL)); 
     try { 
      HttpEntity httpEntity = httpClient.execute(httpGet).getEntity(); 
      if (httpEntity != null) { 
       InputStream inputStream = httpEntity.getContent(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
       final StringBuilder stringBuilder = new StringBuilder(); 
       String cen = null; 

       while ((cen = bufferedReader.readLine()) != null) { 

        /* Intent intent=new Intent(Login.this,MainActivity.class); 
        startActivity(intent);*/ 
        stringBuilder.append(cen + "n"); 
       } 
       inputStream.close(); 

       resp = stringBuilder.toString(); 
       /*text.setText(resp);*/ 
       System.out.println("response" + resp); 

      } 
     } catch (ClientProtocolException e) { 
      Toast.makeText(Login.this, e.toString(), Toast.LENGTH_LONG); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Toast.makeText(Login.this, e.toString(), Toast.LENGTH_LONG); 
     } 
     System.out.println("resp end"); 
     return resp; 



    } 

append()方法

public String append(String url){ 
     url+=userName.getText().toString()+"/"+ password.getText().toString(); 
     System.out.println("username"+userName.getText().toString()); 

     return url; 
    } 

**error** 
W/System.err: org.apache.http.conn.HttpHostConnectException: Connection to http://180.211.68.232:8080 refused 
W/System.err:  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:193) 
W/System.err:  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169) 
W/System.err:  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124) 
W/System.err:  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366) 
W/System.err:  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560) 
W/System.err:  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492) 
W/System.err:  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470) 
W/System.err:  at hello.com.navigationdrawer.Login.resp(Login.java:156) 
W/System.err:  at hello.com.navigationdrawer.Login$ExecuteTask.doInBackground(Login.java:248) 
W/System.err:  at hello.com.navigationdrawer.Login$ExecuteTask.doInBackground(Login.java:242) 
W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:295) 
W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
W/System.err:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
W/System.err:  at java.lang.Thread.run(Thread.java:818) 
W/System.err: Caused by: java.net.ConnectException: failed to connect to /180.211.68.232 (port 8080): connect failed: ETIMEDOUT (Connection timed out) 
W/System.err:  at libcore.io.IoBridge.connect(IoBridge.java:124) 
W/System.err:  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) 
W/System.err:  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452) 
W/System.err:  at java.net.Socket.connect(Socket.java:884) 
W/System.err:  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:124) 
W/System.err:  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:149) 
W/System.err: ... 15 more 
W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out) 
W/System.err:  at libcore.io.Posix.connect(Native Method) 
W/System.err:  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111) 
W/System.err:  at libcore.io.IoBridge.connectErrno(IoBridge.java:137) 
W/System.err:  at libcore.io.IoBridge.connect(IoBridge.java:122) 
W/System.err: ... 20 more 

URL 最後字符串的URL = 「http://180.211.68.232:8080/JIRAservice-1.0-SNAPSHOT/rest/login/」;

+0

使用排球庫.. –

+0

請以正常方式發佈文本。不在代碼塊中。不要讓我們滾動閱讀您的問題。 – greenapps

回答

0

這個錯誤是一個超時錯誤,但即使設置了超時也無法解決。試用不同的超時時間