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条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题