Android应用无法连接到localhost服务器

时间:2016-05-06 11:20:01

标签: java android node.js server

我已经在我的localhost中创建了Database,并希望通过android app.I创建解析数据。使用NodeJS创建服务器并且它可以工作,我可以在浏览器中看到数据库,但是我的android app无法与此localhost服务器连接。 这是我的Java代码。

 public class MainActivity extends AppCompatActivity {

 protected TextView tvData;

 public static ArrayList<String> titleList = new ArrayList<String>();
 private static ArrayAdapter<String> adapter;
 private static ListView lv;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvData = (TextView)findViewById(R.id.tvJsonItem);

   // adapter = new ArrayAdapter<String>(this, R.layout.list_item,   R.id.listView1, titleList);
   // lv = (ListView) findViewById(R.id.listView1);

    new JSONTask().execute("http://10.0.2.2:3000/api/people");

 }

 public class JSONTask extends AsyncTask<String,String,String>{
    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        int statusCode = 0;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestMethod("GET");
            connection.connect(); //connect to server

            statusCode = connection.getResponseCode();


            if (statusCode ==  200) {
                System.out.println("Server responded with code: " + statusCode);

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();
                String line = "";

                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }

                String finalJSON = buffer.toString();
                JSONObject parentObject = new JSONObject(finalJSON);
                JSONArray parentArray = parentObject.getJSONArray("people");

                StringBuffer finalBufferdData = new StringBuffer();

                for (int i = 0; i < parentArray.length(); i++) {

                    JSONObject finalObj = parentArray.getJSONObject(i);

                    String peopleName = finalObj.getString("name");
                    Integer age = finalObj.getInt("age");
                    finalBufferdData.append(peopleName + "-->" + age + "\n");

                    Log.i("Hakob_log",peopleName + "-->" + age + "\n");
                }


                return finalBufferdData.toString(); //pases result to POstExecute
            }else{
                Log.i("Hakob_log","Error");
            }

        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }catch(JSONException e){
            e.printStackTrace();
        }
        finally {
            if(connection !=null) {
                connection.disconnect();
            }
            try {
                if(reader !=null) {
                    reader.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return null;
    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        tvData.setText(result);
    }
}
}

我认为这条线错了

new JSONTask().execute("http://localhost:3000/api/people");

由于

1 个答案:

答案 0 :(得分:0)

使用您所指的计算机的IP地址作为localhost,这应该可以解决您的问题,例如:

$transaction =
array(
 'kind' => 'capture'
    );
  //baseurl is defines properly

 $ch = curl_init($baseUrl.'orders/#3055189382/transactions.json'); //set the url

 $data_string = json_encode(array('transaction'=>$transaction)); //encode the   product as json
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  //specify this as a POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); //set the POST string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //specify return value as string
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
); 
$server_output = curl_exec ($ch);