无法通过php使用android模拟器将数据插入mysql

时间:2014-08-17 11:42:02

标签: php android mysql xml

实际上,我是android的新手,并尝试使用android通过php页面将记录插入到mysql表中。我使用wamp服务器但是。我使用IP地址10.0.2.2而不是127.0.0.1我也使用了10.0.0.2:8676的IP地址作为我的端口号,但它们都没有工作,我总是发现这是错误无效的IP地址。我无法理解我错在哪里。虽然此代码在实时服务器主机上工作,但值插入到实时服务器但不在localhost中。使用wamp服务器,我的操作系统是Windows XP 3。我正在使用Eclipse。 Android SDK 17 Android版4.2。以下是我的代码:

提前致谢。

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(com.example.buddyproject.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();

        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://10.0.2.2/androidtest/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());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}    

}

$host='localhost';
$uname='root';
$pwd='';
$db="test";

$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");


$id=$_REQUEST['id'];
$name=$_REQUEST['name'];

$flag['code']=0;

    $sql="INSERT INTO sample (id,name)VALUES('$id','$name')";



 if (!mysql_query($sql,$con))
{
    $flag['code']=1;
    echo"hi";
}
print(json_encode($flag));
mysql_close($con);

2 个答案:

答案 0 :(得分:0)

您必须在此行中更改您的IP

 HttpPost httppost = new HttpPost("http://10.0.2.2/androidtest/insert.php");

打开cmd-type ipconfig -hit enter。 获取IPv4地址通常以192.X

开头

enter image description here

更改代码中的ip。它应该工作

答案 1 :(得分:0)

您的try语句正在捕获每个异常。你不应该这样做,你当然不能在第一个块中得出“无效的IP地址”。

由于您尝试在UI线程上进行联网,因此您更有可能抛出NetworkOnMainThreadException。你不应该这样做。您应该将网络代码委派给不同的线程,或者在AsyncTask中运行它。