简体   繁体   中英

Connection Refused in reading JSON from PHP in localhost, Android Studio

I'm trying to connect to one php on my localhost server that works ok, but I can't read the content there.

Here is my code:

package com.example.klaussius.probandoconexionservidor;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import serverDataRead.ReadUsers;

public class Principal extends AppCompatActivity {

    TextView tvText;
    Button btGetInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        // TextView for our text
        tvText = (TextView)findViewById(R.id.tvText);
        // Button to get text
        btGetInfo = (Button)findViewById(R.id.btGetInfo);
        btGetInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new leerDatos().execute();
            }
        });
    }

private class leerDatos extends AsyncTask<String,Void,String> {
    @Override
    protected String doInBackground(String... strings) {
        ReadUsers reader = new ReadUsers();
        return reader.readUsers();
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        tvText.setText(s);
        }
    }
}

And my method readUsers is in the class readUsers:

package serverDataRead;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ReadUsers {
    public String texto;

    public String readUsers(){
        try {
            String link = "http://127.0.0.1/api/usersQuery.php";
            URL url = new URL(link);
            HttpURLConnection con = HttpURLConnection)url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            texto = bufferedReader.readLine();
        } catch (Exception e) {
            texto = "Fail!";
            e.printStackTrace();
        } finally {
            return texto;
        }
    }
}

The layout:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btGetInfo"
        android:text="@string/getText" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvText" />

</LinearLayout>

The error I get: W/System.err: java.net.ConnectException: Connection refused

Thanks in advance for your help.

If you are using emulator rather than genymotion then use 10.0.2.2 instead of localhost so the url will be " http://10.0.2.2/api/hostsQuery.php " and if you use genymotion then replace localhost with 10.0.3.2 ,so the url will be "http://10.0.3.2/api/hostsQuery.php"

Let me know if this works for you

解:

HttpURLConnection con = (HttpURLConnection) url.openConnection();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM