1
我遵循互聯網上的例子。當我做的是工作成功,但它不起作用,當我增加變量。它在html中運行良好,並且在我構建手機時沒有錯誤。但是,數據不會傳遞給mysql。如何在Android中連接mysql
當然,我加入了互聯網許可清單
存在的例子,我也跟着互聯網上。它確實很好。
public class MainActivity extends Activity {
private EditText editTextName;
private EditText editTextAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = (EditText) findViewById(R.id.name);
editTextAdd = (EditText) findViewById(R.id.mobile);
}
public void insert(View view){
String name = editTextName.getText().toString();
String mobile = editTextAdd.getText().toString();
insertToDatabase(name, mobile);
}
private void insertToDatabase(String name, String mobile){
class InsertData extends AsyncTask<String, Void, String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
try{
String name = (String)params[0];
String mobile = (String)params[1];
String link="http://gilmucompany.xyz/gg.php";
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8");
data += "&" + URLEncoder.encode("mobile", "UTF-8") + "=" + URLEncoder.encode(mobile, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
}
這個問題
public class Insert extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insertdata);
}
public void Insert(View view){
Intent intent = getIntent();
String time1 = (String) intent.getStringExtra("time1");
String time2 = (String) intent.getStringExtra("time2");
String start = (String) intent.getStringExtra("start");
String destiny = (String) intent.getStringExtra("destiny");
String time = (String) intent.getStringExtra("time");
insertToDatabase(time1,time2,start,destiny,time);
}
private void insertToDatabase(String time1, String time2,String start, String destiny, String time){
class InsertData extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Insert.this, "Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
try{
String time1 = (String)params[0];
String time2 = (String)params[1];
String start = (String)params[2];
String destiny = (String)params[3];
String time = (String)params[4];
String link="www.gilmucompany.xyz/insertinformationdata.php/";
String data = URLEncoder.encode("time1", "UTF-8") + "=" + URLEncoder.encode(time1, "UTF-8");
data += "&" + URLEncoder.encode("time2", "UTF-8") + "=" + URLEncoder.encode(time2, "UTF-8");
data += "&" +URLEncoder.encode("start", "UTF-8") + "=" + URLEncoder.encode(start, "UTF-8");
data += "&" + URLEncoder.encode("destiny", "UTF-8") + "=" + URLEncoder.encode(destiny, "UTF-8");
data += "&" + URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(time, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
}
InsertData task = new InsertData();
task.execute(time1,time2,start,destiny,time);
}
什麼你確切的問題..? – Keerthivasan
在讀取BufferedReader中的數據之前,您需要設置conn.setDoInput(true); – Keerthivasan