package demo.com.mysqldbdemo;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
/**
* Created by Ahmadzai on 7/10/2017.
*/
public class BackgroundTask extends AsyncTask<String, Void, String> {
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://192.168.8.100:8081/webapp/register.php";
String login_url = "http://localhost/webapp/login.php";
String method = params[0];
if (method.equals("register"))
{
String name = params[1];
String user_name = params[2];
String user_pass = params[3];
try {
URL url = new URL(reg_url);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setDoOutput(true);
OutputStream OS = httpsURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("user","UTF-8") + "=" +URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("user_name","UTF-8") + "=" +URLEncoder.encode(user_name,"UTF-8")+"&"+
URLEncoder.encode("user_pass","UTF-8") + "=" +URLEncoder.encode(user_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpsURLConnection.getInputStream();
IS.close();
return "Registration succeed ....";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
我想數據插入MySQL表HTTP參數,如以下代碼沒有任何錯誤,但我按在接口寄存器按鈕應用程序停止運行時,欲調試它運行的應用程序,直到「HttpsURLConnection httpsURLConnection =(HttpsURLConnection)url.openConnection();」之後的代碼行停止調試。 任何人都可以在這方面幫助我嗎?我一直在使用stackoverflow的答案,但這是我第一次嘗試提問,抱歉給您帶來不便。的Android - 發送通過POST方法容易
什麼是您的logcat的說? –
我建議你閱讀關於改造 – DeKaNszn
我打賭類拋出異常,因爲你的URL不代表'HTTPS'連接。 –