制作一个示例应用,用户可以在其中添加ID和名称。但是我没有得到任何错误,也没有得到输出。
这是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 + "\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());
}
}
}
和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);
?>
,错误是:
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
答案 0 :(得分:0)
您正在主线程上调用网络资料(insert()
方法),这在Android开发中是不允许的。尝试使用AsyncTask
包装insert()
逻辑。此外,请考虑您是否已将INTERNET
权限添加到清单文件中。