【发布时间】:2015-09-12 07:09:13
【问题描述】:
我正在尝试使用 json 和 PHP 网络服务将 edittext 值插入到 mysql 表中。我找到了执行此操作的代码;当 web 服务工作时,它应该传递“成功”消息。但是当点击提交按钮时它会强制关闭。这是代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mhost=(EditText) findViewById(R.id.field_host);
mdb=(EditText) findViewById(R.id.field_db);
muser=(EditText) findViewById(R.id.field_user);
mpassword=(EditText) findViewById(R.id.field_password);
mConnection=(Button) findViewById(R.id.insert);
mConnection.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Connect().execute();
}
});
}
class Connect extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
String HOST=mhost.getText().toString();
String USER=muser.getText().toString();
String DB=mdb.getText().toString();
String PASSWORD=mpassword.getText().toString();
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("nfc_tag", HOST));
params.add(new BasicNameValuePair("meter_reading", USER));
params.add(new BasicNameValuePair("reading_datetime", DB));
params.add(new BasicNameValuePair("image_name", PASSWORD));
JSONObject json=jp.makeHttpRequest(connection_url, "POST", params);
try {
int success=json.getInt(Tag_success);
if(success==1){
Toast.makeText(getApplicationContext(), "Connected :-)", Toast.LENGTH_LONG);
}else{
Toast.makeText(getApplicationContext(), "Not Connected :(", Toast.LENGTH_LONG);
}
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
}
JsonParser 类
public class JsonParser {
static InputStream IS=null;
static JSONObject JB=null;
static String json="";
public JsonParser(){
}
public JSONObject makeHttpRequest(String url,String method,List<NameValuePair> params){
try {
if(method=="POST"){
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
}else if(method=="GET"){
DefaultHttpClient httpClient=new DefaultHttpClient();
String paramaString =URLEncodedUtils.format(params, "utf-8");
url +="?"+paramaString;
HttpGet httpGet=new HttpGet(url);
HttpResponse httpresponse=httpClient.execute(httpGet);
HttpEntity httpEntity=httpresponse.getEntity();
IS=httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (ClientProtocolException e) {
// TODO: handle exception
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(IS,"iso-8859"),8);
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
IS.close();
json=sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("Buffer Error","Connection Error"+e.toString());
}
try {
JB=new JSONObject(json);
} catch (JSONException e) {
// TODO: handle exception
Log.e("JSON Parser", e.toString());
}
return JB;
}
PHP 脚本
<?php
$host=$_POST['nfc_tag'];
$user=$_POST['meter_reading'];
$db=$_POST['reading_datetime'];
$password=$_POST['image_name'];
$response=array();
$connect=mysql_connect($host,$user,$password);
if(mysql_select_db($db,$connect)){
$response['success']=1;
}else{
$response['success']=2;
}
echo json_encode($response);
?>
【问题讨论】:
-
请发布异常堆栈跟踪。
-
@kiturk3 LogCat : "09-14 12:02:46.454: E/Buffer Error(6380): Connection Errorjava.lang.NullPointerException: lock == null ", "09-14 12:02 :46.454: E/JSON Parser(6380): org.json.JSONException: 字符 0 处的输入结束 ", "09-14 12:02:46.474: E/AndroidRuntime(6380): java.lang.RuntimeException: An执行 doInBackground() 时发生错误"
标签: android json web-services