在Android Studio中连接HTTP客户端的IP地址无效

时间:2016-09-15 18:31:13

标签: android android-studio android-spinner

这是我希望显示从MySQL到Android微调器的记录的代码但是我在catch异常中遇到错误。错误是:

  

无效的IP地址

以下是代码:

 b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            final List<String> list1 = new ArrayList<String>();
            final List<String> list2 = new ArrayList<String>();

            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/user.php");
                HttpResponse response = httpclient.execute(httppost);
                Log.e("Fail 1", "3");

                HttpEntity entity = response.getEntity();
                Log.e("Fail 1", "4");

                is = entity.getContent();
                Log.e("Pass 1", "connection success ");
            }
            catch(Exception e)
            {
                Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
                finish();
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("Fail 2", e.toString());
            }


            try {
                JSONArray JA = new JSONArray(result);
                JSONObject json = null;
                roll_no = new String[JA.length()];
                name = new String[JA.length()];

                for (int i = 0; i < JA.length(); i++) {
                    json = JA.getJSONObject(i);
                    roll_no[i] = json.getString("FirstName");
                    name[i] = json.getString("LastName");
                }
                Toast.makeText(getApplicationContext(), "sss", Toast.LENGTH_LONG).show();

                for (int i = 0; i < roll_no.length; i++) {
                    list1.add(roll_no[i]);
                    list2.add(name[i]);
                }
                spinner_fn();

            } catch (Exception e) {

                Log.e("Fail 3", e.toString());
                //login.this.finish();

            }

        }
    });
}

private void spinner_fn() {
// TODO Auto-generated method stub

    ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
            android.R.layout.simple_spinner_item, roll_no);
    dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp.setAdapter(dataAdapter1);

}

0 个答案:

没有答案