使用HttpURLConnection的Android设备上的PHP HTTP POST
问题: 所以问题是,这不会发布到goDaddy托管的在线数据库中。所以我的问题是为什么,我如何修复它,让它发布到它 问题: php页面没有收到传递给它的名称-值对 编辑: 修改了建议使用HttpURLConnection的代码。。。 我已经把问题缩小到不检索fbid 正如你所说,我在这里做了很多家庭作业。。。这是我在postthread类中设置的日志,这是我完成post的类:使用HttpURLConnection的Android设备上的PHP HTTP POST,php,android,Php,Android,问题: 所以问题是,这不会发布到goDaddy托管的在线数据库中。所以我的问题是为什么,我如何修复它,让它发布到它 问题: php页面没有收到传递给它的名称-值对 编辑: 修改了建议使用HttpURLConnection的代码。。。 我已经把问题缩小到不检索fbid 正如你所说,我在这里做了很多家庭作业。。。这是我在postthread类中设置的日志,这是我完成post的类: 07-02 16:41:45.108: I/PROJECTCARUSO(12308): response: {"post
07-02 16:41:45.108: I/PROJECTCARUSO(12308): response: {"posts":[null]}
HttpPostThread:
public class HttpPostThread extends Thread {
public static final int FAILURE = 0;
public static final int SUCCESS = 1;
private URL url;
ArrayList<NameValuePair> pairs;
public HttpPostThread(URL sERVICE_URL, ArrayList<NameValuePair> pairs, final Handler handler)
{
Log.i("PROJECTCARUSO", "Posting to URL: " + sERVICE_URL);
this.url =sERVICE_URL;
this.pairs = pairs;
if(pairs==null){
Log.i("PROJECTCARUSO", "URL parms were null");
this.pairs = new ArrayList<NameValuePair>();
}
}
@Override
public void run()
{
try {
HttpURLConnection conn;
String param="";
for (NameValuePair nvp : pairs) {
//you need to encode ONLY the values of the parameters
if (param == "") {
param=nvp.getName() + "=" + URLEncoder.encode(nvp.getValue(),"UTF-8");
} else {
param+= "&" + nvp.getName() + "=" + URLEncoder.encode(nvp.getValue(),"UTF-8");
}
}
Log.i("PROJECTCARUSO", "param: " + param.toString());
// Create connection
conn=(HttpURLConnection)url.openConnection();
//set the output to true, indicating you are outputting(uploading) POST data
conn.setDoOutput(true);
//once you set the output to true, you don't really need to set the request method to post, but I'm doing it anyway
conn.setRequestMethod("POST");
//Android documentation suggested that you set the length of the data you are sending to the server, BUT
// do NOT specify this length in the header by using conn.setRequestProperty("Content-Length", length);
//use this instead.
conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//send the POST out
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();
//build the string to store the response text from the server
String response= "";
//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());
//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());
Log.i("PROJECTCARUSO","response: " + response);
}
//catch some error
catch(MalformedURLException ex){
Log.i("PROJECTCARUSO", ex.toString());
}
// and some more
catch(IOException ex){
Log.i("PROJECTCARUSO", ex.toString());
}
}
public static boolean isNumeric(String str)
{
try
{
double d = Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
}
编辑:
我甚至尝试了java,如下所示:
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (NameValuePair nvp : pairs) {
//you need to encode ONLY the values of the parameters
params.add(new BasicNameValuePair(nvp.getName(), nvp.getValue()));
Log.i("PROJECTCARUSO", "NVP: " + nvp.getName() + " - " + nvp.getValue());
}
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.close();
os.close();
conn.connect();
//build the string to store the response text from the server
String response= "";
//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());
//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());
Log.i("PROJECTCARUSO","response: " + response);
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public void run(){
试一试{
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
连接设置读取超时(10000);
连接设置连接超时(15000);
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
List params=new ArrayList();
用于(NameValuePair nvp:pairs){
//您只需要对参数的值进行编码
add(新的BasicNameValuePair(nvp.getName(),nvp.getValue());
Log.i(“PROJECTCARUSO”,“NVP:+NVP.getName()+”-“+NVP.getValue());
}
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
write(getQuery(params));
writer.close();
os.close();
连接();
//生成字符串以存储来自服务器的响应文本
字符串响应=”;
//开始收听流
Scanner inStream=新扫描仪(连接getInputStream());
//处理流并将其存储在StringBuilder中
while(inStream.hasNextLine())
响应+=(inStream.nextLine());
Log.i(“PROJECTCARUSO”,“response:”+response);
}捕获(协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
假设您使用apache HttpClient作为默认客户端
您需要有关WIRE和Header的更多数据。演示如何打开这些记录器。阅读有关日志记录的apache文档
下面是您将看到的示例(您将确切地知道客户端和服务器之间发生了什么)
完全登录apache HttpClient:
D/ch.boye.httpclientandroidlib.wire( 1175): >> "PUT /1/classes/Books/8NUX0YP5XK HTTP/1.1[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "Content-Length: 375[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "Content-Type: text/plain; charset=ISO-8859-1[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "Host: api.parse.com[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "Connection: Keep-Alive[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "X-Parse-Application-Id: 3[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "X-Parse-REST-API-Key: kVl9[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.headers( 1175): >> PUT /1/classes/Books/8NUX0YP5XK HTTP/1.1
D/ch.boye.httpclientandroidlib.headers( 1175): >> Content-Length: 375
D/ch.boye.httpclientandroidlib.headers( 1175): >> Content-Type: text/plain; charset=ISO-8859-1
D/ch.boye.httpclientandroidlib.headers( 1175): >> Host: api.parse.com
D/ch.boye.httpclientandroidlib.headers( 1175): >> Connection: Keep-Alive
D/ch.boye.httpclientandroidlib.headers( 1175): >> X-Parse-Application-Id: 3K
D/ch.boye.httpclientandroidlib.headers( 1175): >> X-Parse-REST-API-Key: kVl5Z
D/ch.boye.httpclientandroidlib.wire( 1175): >> "{"pages":{"__op":"Add","objects":[{"__type":"Pointer","ClassName":"TestVoiceObject","objectId":"bsKyc8mKV7"},{"__type":"Pointer","ClassName":"TestVoiceObject","objectId":"hehlqEUJw8"},{"__type":"Pointer","ClassName":"TestVoiceObject","objectId":"rtbhCb37tq"},{"__type":"Pointer","ClassName":"TestVoiceObject","objectId":"84zjWpJy6y"}]},"ACL":{"*":{"read":true,"write":true}}}"
D/ch.boye.httpclientandroidlib.wire( 1175): << "HTTP/1.1 200 OK[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Access-Control-Allow-Origin: *[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Access-Control-Request-Method: *[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Cache-Control: max-age=0, private, must-revalidate[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Content-Type: application/json; charset=utf-8[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Date: Mon, 08 Apr 2013 19:51:59 GMT[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "ETag: "172e8ee0c4828b5fce3303c078b8f2ad"[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Server: nginx/1.2.2[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Set-Cookie: _parse_session=BAh7BkkiD3d989bfe; domain=.parse.com; path=/; expires=Sat, 08-Apr-2023 19:51:59 GMT; secure; HttpOnly[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Status: 200 OK[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "X-Runtime: 0.041462[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "X-UA-Compatible: IE=Edge,chrome=1[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Content-Length: 500[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Connection: keep-alive[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "[\r][\n]"
D/class ch.boye.httpclientandroidlib.impl.conn.DefaultClientConnection( 1175): Receiving response: HTTP/1.1 200 OK
D/ch.boye.httpclientandroidlib.headers( 1175): << HTTP/1.1 200 OK
D/ch.boye.httpclientandroidlib.headers( 1175): << Access-Control-Allow-Origin: *
D/ch.boye.httpclientandroidlib.headers( 1175): << Access-Control-Request-Method: *
D/ch.boye.httpclientandroidlib.headers( 1175): << Cache-Control: max-age=0, private, must-revalidate
D/ch.boye.httpclientandroidlib.headers( 1175): << Content-Type: application/json; charset=utf-8
D/ch.boye.httpclientandroidlib.headers( 1175): << Date: Mon, 08 Apr 2013 19:51:59 GMT
D/ch.boye.httpclientandroidlib.headers( 1175): << ETag: "172e8ee0c4828b5fce3303c078b8f2ad"
D/ch.boye.httpclientandroidlib.headers( 1175): << Server: nginx/1.2.2
D/ch.boye.httpclientandroidlib.headers( 1175): << Set-Cookie:
D/ch.boye.httpclientandroidlib.wire(1175):>>“PUT/1/classes/Books/8NUX0YP5XK HTTP/1.1[\r][\n]”
D/ch.boye.httpclientandroidlib.wire(1175):>>“内容长度:375[\r][\n]”
D/ch.boye.httpclientandroidlib.wire(1175):>>“内容类型:text/plain;charset=ISO-8859-1[\r][\n]”
D/ch.boye.httpclientandroidlib.wire(1175):>>“主机:api.parse.com[\r][\n]”
D/ch.boye.httpclientandroidlib.wire(1175):>>“连接:保持活动[\r][\n]”
D/ch.boye.httpclientandroidlib.wire(1175):>>“X-Parse-Application-Id:3[\r][\n]”
D/ch.boye.httpclientandroidlib.wire(1175):>>“X-Parse-REST-API-Key:kVl9[\r][\n]”
D/ch.boye.httpclientandroidlib.wire(1175):>>“[\r][\n]”
D/ch.boye.httpclientandroidlib.headers(1175):>>PUT/1/classes/Books/8NUX0YP5XK HTTP/1.1
D/ch.boye.httpclientandroidlib.headers(1175):>>内容长度:375
D/ch.boye.httpclientandroidlib.headers(1175):>>内容类型:text/plain;charset=ISO-8859-1
D/ch.boye.httpclientandroidlib.headers(1175):>>主机:api.parse.com
D/ch.boye.httpclientandroidlib.headers(1175):>>连接:保持活动
D/ch.boye.httpclientandroidlib.headers(1175):>>X-Parse-Application-Id:3K
D/ch.boye.httpclientandroidlib.headers(1175):>>X-Parse-REST-API-Key:kVl5Z
D/ch.boye.httpclientandroidlib.wire(1175):>“{”pages:{”pages:{uuuu op:“Add”,“objects:[{”\uuu type:“Pointer”,“ClassName:“TestVoiceObject”,“objectId:“bsKyc8mKV7”},{”\uuuu type:“Pointer”,“ClassName:“testvoiceobjectid:”hehlqEUJw8“},{”voiceuuuu type:“Pointer”,“ClassName:“testobjectid:”rtbhcbhcb37tq:“Pointer”,“ClassName:{”TestVoiceObject、objectId:“84zjWpJy6y”}],“ACL:{“*”:{“读”:真,“写”:真}”
D/ch.boye.httpclientandroidlib.wire(1175):您必须使用$\u POST
而不是$\u GET
无处不在。为什么要在变量字符串值周围添加单引号?对于insert语句…在测试它时,我发现,如果它周围没有引号,则在执行insert时,它会失败。因此,它最终会在用户中查找lineinsert(_id,name,…)值('0121','Jason',…)
当我在浏览器中打开页面并添加所有paramaters@Jonast92你的评论没有建设性。但是,如果你确实有一个建议要提,尽管是离题的,我很乐意听到。这是否表示“成功发送请求”?您也不会检查响应是否为200,等等。因此,请验证这一点。此外,将此问题分解为特定的android和php问题。它们不相关。它确实显示“成功发送请求”.我没有打断它,因为它似乎都在android端,但我可能不正确。至于检查响应,您是指插入的sql响应还是指android端的响应?说得太快了…godaddys端有apache日志…让我看看这个,看看上面的编辑。我看到了这个解释:我的错误..看起来不像服务器端问题使用curl从cli测试php。在开始使用android客户端之前,你应该确定你的服务器在响应体中发布了良好的json。使用curl的cli?请解释?这是我能给出的最长答案:)
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (NameValuePair nvp : pairs) {
//you need to encode ONLY the values of the parameters
params.add(new BasicNameValuePair(nvp.getName(), nvp.getValue()));
Log.i("PROJECTCARUSO", "NVP: " + nvp.getName() + " - " + nvp.getValue());
}
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.close();
os.close();
conn.connect();
//build the string to store the response text from the server
String response= "";
//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());
//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());
Log.i("PROJECTCARUSO","response: " + response);
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
D/ch.boye.httpclientandroidlib.wire( 1175): >> "PUT /1/classes/Books/8NUX0YP5XK HTTP/1.1[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "Content-Length: 375[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "Content-Type: text/plain; charset=ISO-8859-1[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "Host: api.parse.com[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "Connection: Keep-Alive[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "X-Parse-Application-Id: 3[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "X-Parse-REST-API-Key: kVl9[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.headers( 1175): >> PUT /1/classes/Books/8NUX0YP5XK HTTP/1.1
D/ch.boye.httpclientandroidlib.headers( 1175): >> Content-Length: 375
D/ch.boye.httpclientandroidlib.headers( 1175): >> Content-Type: text/plain; charset=ISO-8859-1
D/ch.boye.httpclientandroidlib.headers( 1175): >> Host: api.parse.com
D/ch.boye.httpclientandroidlib.headers( 1175): >> Connection: Keep-Alive
D/ch.boye.httpclientandroidlib.headers( 1175): >> X-Parse-Application-Id: 3K
D/ch.boye.httpclientandroidlib.headers( 1175): >> X-Parse-REST-API-Key: kVl5Z
D/ch.boye.httpclientandroidlib.wire( 1175): >> "{"pages":{"__op":"Add","objects":[{"__type":"Pointer","ClassName":"TestVoiceObject","objectId":"bsKyc8mKV7"},{"__type":"Pointer","ClassName":"TestVoiceObject","objectId":"hehlqEUJw8"},{"__type":"Pointer","ClassName":"TestVoiceObject","objectId":"rtbhCb37tq"},{"__type":"Pointer","ClassName":"TestVoiceObject","objectId":"84zjWpJy6y"}]},"ACL":{"*":{"read":true,"write":true}}}"
D/ch.boye.httpclientandroidlib.wire( 1175): << "HTTP/1.1 200 OK[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Access-Control-Allow-Origin: *[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Access-Control-Request-Method: *[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Cache-Control: max-age=0, private, must-revalidate[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Content-Type: application/json; charset=utf-8[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Date: Mon, 08 Apr 2013 19:51:59 GMT[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "ETag: "172e8ee0c4828b5fce3303c078b8f2ad"[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Server: nginx/1.2.2[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Set-Cookie: _parse_session=BAh7BkkiD3d989bfe; domain=.parse.com; path=/; expires=Sat, 08-Apr-2023 19:51:59 GMT; secure; HttpOnly[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Status: 200 OK[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "X-Runtime: 0.041462[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "X-UA-Compatible: IE=Edge,chrome=1[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Content-Length: 500[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "Connection: keep-alive[\r][\n]"
D/ch.boye.httpclientandroidlib.wire( 1175): << "[\r][\n]"
D/class ch.boye.httpclientandroidlib.impl.conn.DefaultClientConnection( 1175): Receiving response: HTTP/1.1 200 OK
D/ch.boye.httpclientandroidlib.headers( 1175): << HTTP/1.1 200 OK
D/ch.boye.httpclientandroidlib.headers( 1175): << Access-Control-Allow-Origin: *
D/ch.boye.httpclientandroidlib.headers( 1175): << Access-Control-Request-Method: *
D/ch.boye.httpclientandroidlib.headers( 1175): << Cache-Control: max-age=0, private, must-revalidate
D/ch.boye.httpclientandroidlib.headers( 1175): << Content-Type: application/json; charset=utf-8
D/ch.boye.httpclientandroidlib.headers( 1175): << Date: Mon, 08 Apr 2013 19:51:59 GMT
D/ch.boye.httpclientandroidlib.headers( 1175): << ETag: "172e8ee0c4828b5fce3303c078b8f2ad"
D/ch.boye.httpclientandroidlib.headers( 1175): << Server: nginx/1.2.2
D/ch.boye.httpclientandroidlib.headers( 1175): << Set-Cookie: