我已经创建了android应用程序,用于向本地主机服务器添加数据。问题是每次我输入数据并提交时,android应用程序中都没有报告任何错误。但是添加到mysql数据库的数据在所有字段中都是空白的。有人知道如何克服这个问题吗?
正如一位同事所问,我已经添加了代码
package localhost80.sample1;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
public class serverLinking extends AsyncTask<String, Void, String>
{
private String link;
private Context context;
private String ed,lno,par,pcs,mts,wt,ct,var,gw,fw,remarks,method;
public serverLinking(Context context)
{
this.context = context;
}
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0)
{
method = arg0[11];
link = "http://192.168.0.104/narmadaa_trial1/android/add.php";
if(method.equals("POST"))
{
ed = arg0[0];
lno = arg0[1];
par = arg0[2];
pcs = arg0[3];
mts = arg0[4];
wt = arg0[5];
ct = arg0[6];
var = arg0[7];
gw = arg0[8];
fw = arg0[9];
remarks = arg0[10];
try
{
URL url = new URL(link);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod(method);
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("ed","UTF-8")+"="+URLEncoder.encode("lno","UTF-8")+"="+URLEncoder.encode("par","UTF-8")+"="+ URLEncoder.encode("pcs","UTF-8")+"="+URLEncoder.encode("mts","UTF-8")+"="+URLEncoder.encode("wt","UTF-8")+"="+ URLEncoder.encode("ct","UTF-8")+"="+URLEncoder.encode("var","UTF-8")+"="+URLEncoder.encode("gw","UTF-8")+"="+ URLEncoder.encode("fw","UTF-8")+"="+URLEncoder.encode("remarks","UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
os.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Data Added Successfully";
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String result)
{
Toast.makeText(context,result,Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Void... values)
{
super.onProgressUpdate(values);
}
}
2条答案
按热度按时间ctehm74n1#
检查查询字符串(字符串数据)。传递参数需要该名称。分隔符是&
值1=值2=值3(十)
value1name=value1&value2name=value2&value3name=value3((o)
例子
检查add.php上收到的数据($\u get或$\u request)
iyfjxgzm2#
非常感谢@meow kim的宝贵建议。我已经四舍五入,并找到了问题的结论,它只是在请求后丢失的标志,使可怕的问题。同时也感谢@magnus eriksson不断提出建议来帮助我。因此,上述的最终代码如下。
java(为了便于识别,刚刚重命名了sample1.java主活动文件)
serverlinking.java(服务器的连接器文件)
android清单.xml
安卓/add.php