Android 无效的IP地址

Android 无效的IP地址,android,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

我正在尝试将数据库中的数据插入服务器。但是我得到了一份工作 异常无效的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());
        }
    }
}
公共类MainActivity扩展活动{
字符串名;
字符串id;
InputStream=null;
字符串结果=null;
字符串行=null;
int代码;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终EditText e_id=(EditText)findViewById(R.id.editText1);
最终EditText e_name=(EditText)findViewById(R.id.editText2);
按钮插入=(按钮)findViewById(R.id.button1);
insert.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
id=e_id.getText().toString();
name=e_name.getText().toString();
插入();
}
});
}
公开作废插入(){
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“id”,id));
添加(新的BasicNameValuePair(“name”,name));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost-HttpPost=newhttppost(“http://***/insert.php”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
Log.e(“通过1”,“连接成功”);
}捕获(例外e){
Log.e(“Fail 1”,e.toString());
Toast.makeText(getApplicationContext(),“无效IP地址”,
Toast.LENGTH_LONG).show();
}     
试一试{
BufferedReader reader=新的BufferedReader
(新的InputStreamReader(is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
Log.e(“通过2”,“连接成功”);
}捕获(例外e){
Log.e(“Fail 2”,e.toString());
}     
试一试{
JSONObject json_data=新的JSONObject(结果);
code=(json_data.getInt(“code”);
如果(代码==1){
Toast.makeText(getBaseContext(),“已成功插入”,
吐司。长度(短)。show();
}否则{
Toast.makeText(getBaseContext(),“抱歉,再试一次”,
Toast.LENGTH_LONG).show();
}
}捕获(例外e){
Log.e(“Fail 3”,e.toString());
}
}
}
我是新来的。出于安全原因,我不能把我的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) {
        // 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();

}
公共类MainActivity扩展活动{
字符串名;
字符串id;
InputStream=null;
字符串结果=null;
字符串行=null;
int代码;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终EditText e_id=(EditText)findViewById(R.id.editText1);
最终EditText e_name=(EditText)findViewById(R.id.editText2);
按钮插入=(按钮)findViewById(R.id.button1);
insert.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
id=e_id.getText().toString();
name=e_name.getText().toString();
新建loadData().execute();
}
});
}
类loadData扩展异步任务{
私家侦探;
私人关系;
专用HttpResponse请求;
私有输入流是;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Toast.makeText(getApplicationContext(),“Start”,Toast.LENGTH\u LONG.show();
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“id”,id));
添加(新的BasicNameValuePair(“name”,name));
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://xxxxx/insert.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
InputStreamReader ireader=新的InputStreamReader(is);
BufferedReader bf=新的BufferedReader(ireader);
sb=新的StringBuilder();
字符串行=null;
而((line=bf.readLine())!=null){
某人附加(行);
}
Log.e(“通过1”,“连接成功”);
}
捕获(例外e)
{
Log.e(“Fail 1”,e.toString());
Toast.makeText(getApplicationContext(),“无效IP地址”,
Toast.LENGTH_LONG).show();
}
返回id;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
pr.解雇();
Toast.makeText(getApplicationContext(),“End”,Toast.LENGTH_LONG.show();
}
} }

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

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();

}
公共类MainActivity扩展活动{
字符串名;
字符串id;
InputStream=null;
字符串结果=null;
字符串行=null;
int代码;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终EditText e_id=(EditText)findViewById(R.id.editText1);
最终编辑文本e_name=(编辑文本)findViewById(R.id.edit
@Override
public void onClick(View v) {

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

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