无效的IP地址android

时间:2014-05-19 07:04:46

标签: android

我正在尝试将数据从我的数据库插入服务器。但我得到了一个 例外无效的IP地址。我的IP是全球IP,可以从任何地方访问

public class MainActivity extends Activity {

    String name;
    String id;
    InputStream is=null;
    String result=null;
    String line=null;
    int code;

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

        final EditText e_id=(EditText) findViewById(R.id.editText1);
        final EditText e_name=(EditText) findViewById(R.id.editText2);
        Button insert=(Button) findViewById(R.id.button1);

        insert.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            id = e_id.getText().toString();
            name = e_name.getText().toString();

            insert();
        }
    });
    }

    public void insert() {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("id",id));
        nameValuePairs.add(new BasicNameValuePair("name",name));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://****/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        } catch(Exception e) {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
        }     

        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();
            Log.e("pass 2", "connection success ");
        } catch(Exception e) {
            Log.e("Fail 2", e.toString());
        }     

        try {
            JSONObject json_data = new JSONObject(result);
            code=(json_data.getInt("code"));

            if(code==1) {
                Toast.makeText(getBaseContext(), "Inserted Successfully",
                Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getBaseContext(), "Sorry, Try Again",
                Toast.LENGTH_LONG).show();
            }
        } catch(Exception e) {
            Log.e("Fail 3", e.toString());
        }
    }
}

我是新手。出于安全考虑,我无法放入我的IP。请帮帮我。

2 个答案:

答案 0 :(得分:3)

中运行
public class MainActivity extends Activity {

String name;
String id;
InputStream is=null;
String result=null;
String line=null;
int code;

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

    final EditText e_id=(EditText) findViewById(R.id.editText1);
    final EditText e_name=(EditText) findViewById(R.id.editText2);
    Button insert=(Button) findViewById(R.id.button1);

    insert.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


          id = e_id.getText().toString();
          name = e_name.getText().toString();
        new loadData().execute();
    }
});
}


class loadData extends AsyncTask<String, Integer, String> {
private StringBuilder sb;
private ProgressDialog pr;
private HttpResponse req;
private InputStream is;

@Override
protected void onPreExecute() {
    super.onPreExecute();
Toast.makeText(getApplicationContext(), "Start", Toast.LENGTH_LONG).show();
}

@Override
protected String doInBackground(String... arg0) {

      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("id",id));
        nameValuePairs.add(new BasicNameValuePair("name",name));

            try
            {
            HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://xxxxx/insert.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                InputStreamReader ireader = new InputStreamReader(is);
                BufferedReader bf = new BufferedReader(ireader);
                sb = new StringBuilder();
                String line = null;
                while ((line = bf.readLine()) != null) {
                    sb.append(line);
                }
                Log.e("pass 1", "connection success ");
        }
            catch(Exception e)
        {
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid IP Address",
                Toast.LENGTH_LONG).show();
        }
            return id;     





}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    pr.dismiss();
    Toast.makeText(getApplicationContext(), "End", Toast.LENGTH_LONG).show();

}

} }

在其主线程上执行网络操作。在AsyncTask中运行代码

答案 1 :(得分:2)

  

android.os.NetworkOnMainThreadException

这意味着您无法通过UI线程与网络进行通信, 所以,你必须使用async task

创建asyncTask类:

class insertTask extends AsyncTask<String, String, String> {
     protected String doInBackground(String... params) {
         String result = insert();
         return result ;
     }



     protected void onPostExecute(String result) {
         if(result.equals("OK"))....
         //after background is done, use this to show or hide dialogs
     }
 }

您需要在onClick()

执行任务
@Override
public void onClick(View v) {

    id = e_id.getText().toString();
    name = e_name.getText().toString();

    new insertTask().execute("");
}

并修改public void insert()以返回一些字符串,告诉状态是什么,OK,ERR,......