java连接安卓和php mySQL
我是安卓编程的新手。我正在尝试创建一个应用程序,将与互联网上的数据库进行通信。作为帮助,我正在使用来自安卓hive的项目。 首先,我想创建一个应用程序,将一些数据发送到本地主机上的数据库。我正在使用WAMP
在运行我的应用程序并插入要存储在db上的数据后,我遇到了一系列异常,我的应用程序崩溃。 例如:
- E/Buffer Error(545):转换结果java时出错。lang.NullPointerException
- E/JSON解析器(545):解析数据组织时出错。json。JSONException:在的字符0处结束输入
- E/AndroidRuntime(545):致命异常:异步任务#1
- E/AndroidRuntime(545):java。lang.RuntimeException:执行doInBackground()时出错
还有很多其他的例外
我的php脚本(或任何你想叫它们的)工作正常,但我想问题出在JSONParser或其他安卓活动中。。。我一直在寻找解决方案,但我找不到,或者我无法理解它。另外,我从安卓hive下载了代码,但在我的计算机上无法运行。与我的项目类似的问题
创建订单(产品或其他产品)的活动:
package com.example.heywaiter;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import 安卓.app.Activity;
import 安卓.app.ProgressDialog;
import 安卓.content.Intent;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
public class NewProductActivity extends Activity{
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputTablle;
EditText inputOrder;
EditText inputQuantity;
Button btnOrder;
private static String url_create_product = "http://127.0.0.1/安卓_connect/create_product.php";
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_product);
inputTablle = (EditText) findViewById(R.id.inputTablle);
inputOrder = (EditText) findViewById(R.id.inputOrder);
inputQuantity = (EditText) findViewById(R.id.inputQuantity);
btnOrder = (Button) findViewById(R.id.btnOrder);
btnOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CreateNewProduct().execute();
}
});
}
内部类:
class CreateNewProduct extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewProductActivity.this);
pDialog.setMessage("Sending order...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
String tablle = inputTablle.getText().toString();
String drink = inputOrder.getText().toString();
String quantity = inputQuantity.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tablle", tablle));
params.add(new BasicNameValuePair("drink", drink));
params.add(new BasicNameValuePair("quantity", quantity));
JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);
Log.d("Create Response", json.toString());
try{
int success = json.getInt(TAG_SUCCESS);
if(success ==1){
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}else{
}
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
}
}
}
JSONParser
package com.example.heywaiter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import 安卓.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 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) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
如果您需要我的安卓Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.heywaiter"
安卓:versionCode="1"
安卓:versionName="1.0" >
<uses-sdk
安卓:minSdkVersion="8"
安卓:targetSdkVersion="14" />
<application
安卓:allowBackup="true"
安卓:icon="@drawable/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme" >
<activity
安卓:name="com.example.heywaiter.Splash"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
安卓:name="com.example.heywaiter.MainActivity"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="com.example.heywaiter.MainActivity" />
<category 安卓:name="安卓.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
安卓:name=".NewProductActivity"
安卓:label="New Product">
</activity>
<activity
安卓:name=".AllProductsActivity"
安卓:label="All Products" >
</activity>
</application>
<!-- Internet Permissions -->
<uses-permission 安卓:name="安卓.permission.INTERNET" />
<uses-permission 安卓:name="安卓.permission.ACCESS_WIFI_STATE" />
</manifest>
提前感谢!:)
# 1 楼答案
将其用于保存到数据库
# 2 楼答案
我花了一段时间才发现问题出在哪里,我简直不敢相信问题出在哪里
连接到本地主机而不是:
在NewProductActivity中。java类应使用以下代码:
由于某些原因,您不能使用127.0.0.1,但使用10.0.2.2一切都会很好