dongqin1167
dongqin1167
2016-11-06 13:08

连接拒绝在本地主机,Android Studio中从PHP读取JSON

已采纳

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongquming3255 dongquming3255 5年前

    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

    点赞 评论 复制链接分享
  • douyun3631 douyun3631 4年前

    Solution:

    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    
    点赞 评论 复制链接分享