doujing3896 2015-01-31 04:07
浏览 79

Android:无法使用php在mysql数据库中插入数据

Am making a sample app where the user can add id and name . But the i don't get any errors nor i get the output.

Here's the source code for java:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class MainActivity extends ActionBarActivity {

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

                insert();
            }
        });
    }

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

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

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.1.101/API/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 + "
");
            }
            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());
        }
    }




}

and the insert.php

<?php
        $host='localhost';
        $uname='root';
        $pwd='john123';
        $db="mydatabase";

        $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;

        //if($r=mysql_query("insert into sample values('$id','$name') ",$con))
        if($r=mysql_query("insert into sample (id,name) values('$id','$name') ",$con))
        {
                $flag['code']=1;
                echo"hi";
        }

        print(json_encode($flag));
        mysql_close($con);
?>

and the error is :

0000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=9150448, downTime=9150174, deviceId=0, source=0x101 }
01-30 07:48:47.762      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 07:48:47.871      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 07:48:47.871      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 07:52:36.521      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 07:52:36.571      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 07:52:36.571      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 07:52:38.921      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 07:52:38.941      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 07:52:38.941      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 07:52:41.761      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 07:52:41.781      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 07:52:41.781      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 07:52:42.771      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 07:52:42.822      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 07:52:42.822      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 08:01:32.341      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 08:01:32.391      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 08:01:32.391      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 08:01:35.532      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 08:01:35.581      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 08:01:35.581      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 08:02:23.291      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 08:02:23.421      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 08:02:23.421      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 08:02:27.131      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 08:02:27.251      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 08:02:27.251      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
01-30 08:06:30.621      982-982/com.example.john.mysql_insert E/Fail 1﹕ android.os.NetworkOnMainThreadException
01-30 08:06:30.841      982-982/com.example.john.mysql_insert E/Fail 2﹕ java.lang.NullPointerException: lock == null
01-30 08:06:30.841      982-982/com.example.john.mysql_insert E/Fail 3﹕ java.lang.NullPointerException
  • 写回答

1条回答 默认 最新

  • duanlangwen9597 2015-01-31 05:23
    关注

    You are calling network stuff (insert() method) on main thread, which is not allowed in Android development. Try using AsyncTask to wrap insert() logic. Besides, consider if you already added INTERNET permission to your manifest file.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档