dongqin1167 2016-11-06 13:08
浏览 73
已采纳

连接拒绝在本地主机,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 2016-11-06 13:17
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题