我正在尝试通过Android应用程序在我的在线服务器上传数据,所有的PHP代码和数据库都经过测试他们没问题但是android应用程序没有在Php mysql数据库中上传数据。 SendActivity正在使用Edittext代码,然后在转换为字符串之后将其发送到BackgroundTask类,并通过backgrorund任务将其上载到服务器上,而该服务器现在还没有发生。
SendActivity.java
public void sendbtn(View view)
{
u_name = name.getText().toString();
u_email = email.getText().toString();
u_city = city.getText().toString();
u_address = address.getText().toString();
u_phone= city.getText().toString();
u_altEmail = altEmail.getText().toString();
String basit = "Basit";
//String method = "register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(basit,basit,basit,basit,basit,basit,basit,basit);
finish();
}
BackgroundTask.java
public class BackgroundTask extends AsyncTask<String,Void,String>
{
String add_info_url;
private Context context;
//in constructor:
public BackgroundTask(Context context){
this.context=context;
}
@Override
protected void onPreExecute()
{
add_info_url = "http://ezprint.com.pk/add_info.php";
}
@Override
protected String doInBackground(String... args)
{
String name = args[0];
String email = args[1];
String phone = args[2];
String altaddress = args[3];
String city = args[4];
String address = args[5];
String imgCount = args[6];
String imagesize = args[7];
try {
URL url = new URL(add_info_url);
HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter buffer = new BufferedWriter(new OutputStreamWriter(OS , "UTF-8"));
String data = URLEncoder.encode("name" , "UTF-8")+"="+URLEncoder.encode(name , "UTF-8")+"&"+
URLEncoder.encode("email" , "UTF-8")+"="+URLEncoder.encode(email , "UTF-8")+"&"+
URLEncoder.encode("city" , "UTF-8")+"="+URLEncoder.encode(city , "UTF-8")+"&"+
URLEncoder.encode("address" , "UTF-8")+"="+URLEncoder.encode(address , "UTF-8")+"&"+
URLEncoder.encode("altaddress" , "UTF-8")+"="+URLEncoder.encode(altaddress , "UTF-8")+"&"+
URLEncoder.encode("phone" , "UTF-8")+"="+URLEncoder.encode(phone , "UTF-8")+"&"+
URLEncoder.encode("images" , "UTF-8")+"="+URLEncoder.encode(imgCount , "UTF-8")+"&"+
URLEncoder.encode("imagesize" , "UTF-8")+"="+URLEncoder.encode(imagesize , "UTF-8");
buffer.write(data);
buffer.flush();
buffer.close();
OS.close();
InputStream is = httpURLConnection.getInputStream();
is.close();
httpURLConnection.disconnect();
return "Order is placed successfully";
} 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(context,result,Toast.LENGTH_LONG).show();
}
}
错误日志
12-01 19:31:28.450 9688-12744/org.app.ezprints.photoshopX I/System.out: [socket][0] connection ezprint.com.pk/104.223.95.197:80;LocalPort=36287(0)
12-01 19:31:28.450 9688-12744 / org.app.ezprints.photoshopX I / System.out:[CDS] connect [ezprint.com.pk/104.223.95.197:80] 12-01 19:31:28.730 9688-12744 / org.app.ezprints.photoshopX I / System.out: [CDS] port [36287] 12-01 19:31:28.730 9688-12744 / org.app.ezprints.photoshopX I / System.out:[socket] [/ 192.168.0.107:36287]已连接 12-01 19:31:28.730 9688-12744 / org.app.ezprints.photoshopX I / System.out:[CDS] rx timeout:0 12-01 19:31:29.130 9688-12744 / org.app.ezprints.photoshopX I / System.out:[CDS] rx timeout:100 12-01 19:31:29.130 9688-12744 / org.app.ezprints.photoshopX I / System.out:[CDS] rx timeout:100 12-01 19:31:29.130 9688-12744 / org.app.ezprints.photoshopX I / System.out:[CDS] rx timeout:100 12-01 19:31:29.130 9688-12744 / org.app.ezprints.photoshopX I / System.out:[CDS] rx timeout:0
答案 0 :(得分:4)
您设置httpURLConnection.setRequestMethod("GET");
,但您想上传一些数据。
因此,您应将请求方法更改为POST
。
答案 1 :(得分:0)
确保在AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />