client.connect()无效

时间:2016-07-23 15:54:48

标签: android httpurlconnection connect

我一直在尝试构建一个Android应用程序。我正在尝试将“mobile_num”存储在数据库中。我正在使用GET方法发送数据。但是,client.connect()不会工作。我尽力纠正它并发现错误,但我的所有尝试都失败了。我想就如何纠正错误提出一些建议。

代码:

公共类Mobile_num扩展了AppCompatActivity {

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mobile_num);
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

public void m_sign_in(View view) {
    final EditText num = (EditText) findViewById(R.id.txt_mob);
    try {
        String mob_val = URLEncoder.encode(num.getText().toString(), "UTF-8");

        RequestParams params = new RequestParams();
        params.put("app", "user");
        params.put("method", "register");
        params.put("mobile_no", mob_val);

        String tag = "play";
        Log.i(tag, "num :" + params);

        new signin_process().execute(params);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Mobile_num Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app URL is correct.
            Uri.parse("android-app://com.example.playtym.playtym/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Mobile_num Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app URL is correct.
            Uri.parse("android-app://com.example.playtym.playtym/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}

private class signin_process extends AsyncTask<RequestParams, Void, String> {
    @Override
    protected String doInBackground(RequestParams... params) {
        try {
            //RequestParams params = new RequestParams();

            String tag = "play";
            Log.i(tag, "Async :" + Arrays.toString(params));

            get_response(params);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    private void get_response(RequestParams[] params) throws IOException {
        try {
            String tag = "play";
            Log.i(tag, "get_response :" + Arrays.toString(params));

            String str = Arrays.toString(params);
            // str.replaceAll("/^[[]$/", "");

            String paramstr = URLEncoder.encode(Arrays.toString(params), "UTF-8");
            paramstr= paramstr.replaceAll("%5D","");
            paramstr= paramstr.replaceAll("%5B","");
            paramstr= paramstr.replaceAll("%3D","=");
            paramstr= paramstr.replaceAll("%26","&");

            Log.i(tag, "parm_str : " + paramstr);


            //URL url = new URL("http://192.168.0.111/playtym/v1/int/api.php");
          // String url_s = "http://192.168.0.111/playtym/v1/int/api.php";
            String url_s = "http://localhost/int/api.php";

            url_s += "?" + paramstr;
            //System.out.println(new java.net.URI(url_s).getPath());

            Log.i(tag, "url_s :" + url_s);

            URL url = new URL(url_s);

            HttpURLConnection client = (HttpURLConnection) url.openConnection();
            client.setRequestMethod("GET");
            client.setDoOutput(true);
            client.setDoInput(true);
            client.setRequestProperty("Content-Type", "application/json");
            //request
            // OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
            //out.write(params.toString());
            client.connect();


            // InputStreamReader in = new InputStreamReader()

            int status = client.getResponseCode();
            Log.i(tag, "Status :" + status);

        } catch (ConnectException e)
        {
            Log.e("Play", "Exe", e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

}

ERROR:

  

07-23 23:53:26.511:E / Play(23879):Exe

     

07-23 23:53:26.511:E / Play(23879):java.net.ConnectException:30000ms后无法连接到localhost / 127.0.0.1(端口80):isConnected失败:ECONNREFUSED(拒绝连接)< / p>      

07-23 23:53:26.511:E / Play(23879):at libcore.io.IoBridge.isConnected(IoBridge.java:241)

     

07-23 23:53:26.511:E / Play(23879):at libcore.io.IoBridge.connectErrno(IoBridge.java:174)

     

07-23 23:53:26.511:E / Play(23879):at libcore.io.IoBridge.connect(IoBridge.java:122)

     

07-23 23:53:26.511:E / Play(23879):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)

     

07-23 23:53:26.511:E / Play(23879):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)

     

07-23 23:53:26.511:E / Play(23879):at java.net.Socket.connect(Socket.java:882)

     

07-23 23:53:26.511:E / Play(23879):at com.android.okhttp.internal.Platform.connectSocket(Platform.java:139)

     

07-23 23:53:26.511:E / Play(23879):at com.android.okhttp.Connection.connect(Connection.java:152)

     

07-23 23:53:26.511:E / Play(23879):at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:279)

     

07-23 23:53:26.511:E / Play(23879):at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:213)

     

07-23 23:53:26.511:E / Play(23879):at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)

     

07-23 23:53:26.511:E / Play(23879):at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)

     

07-23 23:53:26.511:E / Play(23879):at com.example.playtym.playtym.Mobile_num $ signin_process.get_response(Mobile_num.java:154)

     

07-23 23:53:26.511:E / Play(23879):at com.example.playtym.playtym.Mobile_num $ signin_process.doInBackground(Mobile_num.java:109)

     

07-23 23:53:26.511:E / Play(23879):at com.example.playtym.playtym.Mobile_num $ signin_process.doInBackground(Mobile_num.java:100)

     

07-23 23:53:26.511:E / Play(23879):在android.os.AsyncTask $ 2.call(AsyncTask.java:288)

     

07-23 23:53:26.511:E / Play(23879):at java.util.concurrent.FutureTask.run(FutureTask.java:237)

     

07-23 23:53:26.511:E / Play(23879):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)

     

07-23 23:53:26.511:E / Play(23879):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

     

07-23 23:53:26.511:E / Play(23879):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)

     

07-23 23:53:26.511:E / Play(23879):at java.lang.Thread.run(Thread.java:818)

     

07-23 23:53:26.511:E / Play(23879):引起:android.system.ErrnoException:isConnected失败:ECONNREFUSED(拒绝连接)

     

07-23 23:53:26.511:E / Play(23879):at libcore.io.IoBridge.isConnected(IoBridge.java:226)

     

07-23 23:53:26.511:E / Play(23879):... 20多

1 个答案:

答案 0 :(得分:0)

String url_s = "http://localhost/int/api.php";

您的手机是否正在运行服务器?您正在获得拒绝连接,因为localhost是您运行该代码的设备。