Java 数据未通过android应用程序插入mysql数据库
我是android新手,当我试图从中插入数据时 我的android应用程序连接到localhost MySQL数据库,应用程序将自动终止。 MainActivity.javaJava 数据未通过android应用程序插入mysql数据库,java,android,mysql,android-asynctask,Java,Android,Mysql,Android Asynctask,我是android新手,当我试图从中插入数据时 我的android应用程序连接到localhost MySQL数据库,应用程序将自动终止。 MainActivity.java public class MainActivity extends Activity { EditText ET_id,ET_FIRSTPLACE,ET_SECONDPLACE,ET_DISTANCE,ET_TIME2REACH,ET_FARE; Button BT_SUBMIT; String id,firstpla
public class MainActivity extends Activity
{
EditText ET_id,ET_FIRSTPLACE,ET_SECONDPLACE,ET_DISTANCE,ET_TIME2REACH,ET_FARE;
Button BT_SUBMIT;
String id,firstplace,secondplace,distance,time2reach,fare;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET_id=(EditText)findViewById(R.id.id);
ET_FIRSTPLACE=(EditText)findViewById(R.id.firstplace);
ET_SECONDPLACE=(EditText)findViewById(R.id.secondplace);
ET_DISTANCE=(EditText)findViewById(R.id.distance);
ET_TIME2REACH=(EditText)findViewById(R.id.time2reach);
ET_FARE=(EditText)findViewById(R.id.fare);
BT_SUBMIT=(Button)findViewById(R.id.submit);
}
public void submit(View view)
{
id=ET_id.getText().toString();
firstplace=ET_FIRSTPLACE.getText().toString();
secondplace=ET_SECONDPLACE.getText().toString();
distance =ET_DISTANCE.getText().toString();
time2reach=ET_TIME2REACH.getText().toString();
fare=ET_FARE.getText().toString();
String method="submit";
BackgroundTask backgroundTask= new BackgroundTask(this);
backgroundTask.execute(method,id,firstplace,secondplace,distance,time2reach,fare);
finish();
}
}
BackgroundTask.java
/**
* Created by Sumedh on 29/08/2015.
*/
public class BackgroundTask extends AsyncTask <String,Void,String> {
Context ctx;
BackgroundTask(Context ctx) {
this.ctx = ctx;
}
protected String doInBackground(String... params)
{
String reg_url = "http://10.0.3.2/busspotter/insert.php";
String method=params[0];
String id =params[1];
String firstplace =params[2];
String secondplace =params[3];
String distance =params[4];
String time2reach =params[5];
String fare =params[6];
if(method.equals("submit")) {
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
//httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
URLEncoder.encode("firstplace","UTF-8")+"="+URLEncoder.encode(firstplace,"UTF-8")+"&"+
URLEncoder.encode("secondplace","UTF-8")+"="+URLEncoder.encode(secondplace,"UTF-8")+"&"+
URLEncoder.encode("distance","UTF-8")+"="+URLEncoder.encode(distance,"UTF-8")+"&"+
URLEncoder.encode("time2reach","UTF-8")+"="+URLEncoder.encode(time2reach,"UTF-8")+"&"+
URLEncoder.encode("fare","UTF-8")+"="+URLEncoder.encode(fare,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
inputStream.close();
httpURLConnection.disconnect();
return "Inserted Succesfully...";
} catch (MalformedInputException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
if (result.equals("Inserted Succesfully...")) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
}
你的
方法返回null,并且您的onPostExecuteString结果尝试从null对象读取值
试着加上这个
if (result != null && result.equals("Inserted Succesfully...")) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} else Toast.makeText(ctx, "Something went wrong", Toast.LENGTH_LONG).show();
您的android在发送http请求时是否连接到wifi?由于您似乎是在一个私人局域网地址上发出请求。我尝试了您的解决方案,但不起作用,应用程序显示出一些问题并被终止。
protected String doInBackground(String... params)
if (result != null && result.equals("Inserted Succesfully...")) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} else Toast.makeText(ctx, "Something went wrong", Toast.LENGTH_LONG).show();