java.net.ConnectException:无法连接到/10.0.0.2(端口80):连接失败:ETIMEDOUT(连接超时)

时间:2015-10-05 17:08:28

标签: java android json

尝试从我的笔记本电脑上运行的XAMPP服务器获取JSON数据,这只是一个测试应用程序,没有重要数据,我有一个主要活动,一个用于获取数据的HttpManager类,一个{ {1}}我正在使用解析数据,我有一个UserdataJson POJO,代码如下。

MainActivity:

UserData

HttpManager:

public class MainActivity extends AppCompatActivity {
    TextView output;
    ProgressBar pb;
    Button btn;
    List<UserData> userDataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        output=(TextView) findViewById(R.id.textView);
        output.setMovementMethod(new ScrollingMovementMethod());
        pb= (ProgressBar) findViewById(R.id.progressBar);
        pb.setVisibility(View.INVISIBLE);
        btn= (Button) findViewById(R.id.button);
        onClicked();

    }

    public void onClicked(){
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOnline()) {
                    requestData("http://10.0.0.2/database/getInfoDroid.php");
                } else {
                    Toast.makeText(MainActivity.this, "Network Unavailable", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        int id = item.getItemId();


        if (id == R.id.action_settings) {

        }

        return super.onOptionsItemSelected(item);
    }

    private void requestData(String uri) {
        MyTask task=new MyTask();
        task.execute(uri);
    }

    protected void updateDisplay(){
        if (userDataList!=null)
        {
            for (UserData userdata:userDataList
                 ) {
                output.append(userdata.getName() + "\n");


            }
        }


    }

    protected boolean isOnline(){
        ConnectivityManager cm= (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo netinfo= cm.getActiveNetworkInfo();
        if(netinfo!= null && netinfo.isConnectedOrConnecting()){
            return true;
        }
        else {
            return false;
        }



    }


    private class MyTask extends AsyncTask {

        @Override
        protected void onPreExecute() {
           // updateDisplay("Background Task Started");
            pb.setVisibility(View.VISIBLE);
        }


        @Override
        protected Object doInBackground(Object[] objects) {

            String content =HttpManager.getData(objects[0].toString());
            return content;


        }

        @Override
        protected void onPostExecute(Object o) {



            userDataList= UserDataJsonParser.parseFeed(o.toString());
            updateDisplay();
            pb.setVisibility(View.INVISIBLE);
        }


        @Override
        protected void onProgressUpdate(Object[] values) {
            //updateDisplay(values[0].toString());
        }
    }



}

JsonParser类:

public class HttpManager {


    public static String getData(String uri){
        StringBuilder sb=new StringBuilder();


        BufferedReader reader=null;
        try {
            URL url=new URL(uri);
            HttpURLConnection con= (HttpURLConnection) url.openConnection();
            reader=new BufferedReader(new InputStreamReader(con.getInputStream()));

            String line;
            while ((line=reader.readLine())!=null){
                sb.append(line+ "\n");

            }
            return sb.toString();

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


}

JSon数据我试图解析:

public class UserDataJsonParser {
    public static List<UserData> parseFeed(String content){
        System.out.println(content);
        try {
            JSONArray array = new JSONArray(content);


            List<UserData> dataList= new ArrayList<>();
            JSONObject obj=null;

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

                obj = array.getJSONObject(i);


                UserData userData = new UserData();
                userData.setId(obj.getInt("id"));
                userData.setName(obj.getString("name"));
                userData.setSurname(obj.getString("surname"));
                userData.setUsername(obj.getString("username"));
                userData.setPassword(obj.getString("password"));

                dataList.add(userData);

            }
            return dataList;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

    }
}

logcat的:

[{"id":"1","name":"Hilary","surname":"Mwape","username":"Hilz","password":"milan"},{"id":"2","name":"John","surname":"Kalyondo","username":"jk","password":"jk92"},{"id":"3","name":"Sipiwe","surname":"Tembo","username":"ST","password":"ST91"}] 

1 个答案:

答案 0 :(得分:2)

终于找到了一个解决方案,Everyone说使用10.0.0.2但是我的URL中出现了上述错误但是使用了我的Pc实际ip n就像魅力一样