2016-07-23 35 views
0

Ich habe versucht, eine Android-Anwendung zu bauen. Ich versuche das "mobile_num" in der Datenbank zu speichern. Ich verwende GET-Methode, um die Daten zu senden. Client.connect() funktioniert jedoch nicht. Ich habe alles versucht, um es zu korrigieren und den Fehler zu finden, aber alle meine Versuche sind gescheitert. Ich hätte gerne Vorschläge, wie die Fehler behoben werden können.client.connect() funktioniert nicht

Code:

public class Mobile_num erweitert 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: failed to connect to localhost/127.0.0.1 (port 80) after 30000ms: isConnected failed: ECONNREFUSED (Connection refused)

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): at 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): at 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): Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)

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 more

+0

Sie besser erwähnen, was der Fehler ist. – muratgu

+0

Hallo Muratgu, ich habe den Fehler geschrieben .. – Varsha

+0

Verbindung abgelehnt, es erscheint – Eenvincible

Antwort

0

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

Läuft auf Ihrem Telefon ein Server? Sie erhalten die Verbindung zurückgewiesen, da localhost Ihr Gerät ist, auf dem Sie diesen Code ausführen.