Sunday, October 17, 2010

[android-developers] Re: I'm so confused with this problem~~~about urlConnection

Two observations:

1) One should post all text entirely in English in this forum. This is
an English-only forum, after all. On top of that, more people will be
able to read your code if the text and comments are in good English.

2) Rather than just say, "the server does not even receive the POST",
you should use Wireshark or tcpdump (if client and server are on
separate network nodes) and or LiveHTTPHeaders/Firebug to monitor the
traffic between client and server. Then tell us what traffic DOES
occur.

3) Finally, did you verify that execution even reaches the line that
prints, "Contents of Post Request"? What about to the line
DataOutputStream.writeBytes()? How WERE you expecting it to get from
sendByPost to this line anyway?

People are more likely to help you out if you show that you have
already done at least this much investigation on your own before
posting.

On Oct 15, 11:25 am, 小贼 <znn...@gmail.com> wrote:
> ============THIS IS ConnectNet .java======================
> package com.zhang7nan;
>
> import java.io.BufferedReader;
>
> import java.io.DataOutputStream;
> import java.io.IOException;
> import java.io.InputStreamReader;
>
> import java.net.HttpURLConnection;
> import java.net.URL;
> import java.net.URLEncoder;
>
> public class ConnectNet {//extends Activity {
>
> public static final String POST_URL = "http://10.0.2.2/date.php";
>
> public static void readContentFromPost() throws IOException {
> // Post请求的url,与get不同的是不需要带参数
> URL postUrl = new URL(POST_URL);
> // 打开连接
> HttpURLConnection connection = (HttpURLConnection) postUrl
> .openConnection();
>
> connection.setDoOutput(true);
> // Read from the connection. Default is true.
> connection.setDoInput(true);
> // Set the post method. Default is GET
> connection.setRequestMethod("POST");
> // Post cannot use caches
> // Post 请求不能使用缓存
> connection.setUseCaches(false);
>
> connection.setInstanceFollowRedirects(true);
>
> connection.setRequestProperty("Content-Type",
> "application/x-www-form-urlencoded");
> // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
> // 要注意的是connection.getOutputStream会隐含的进行connect。
> connection.connect();
>
> DataOutputStream out = new DataOutputStream(connection
> .getOutputStream());
> // The URL-encoded contend
> // 正文,正文内容其实跟get的URL中'?'后的参数字符串一致
> String content = "firstname=" + URLEncoder.encode("wo le ge qu",
> "utf-8");
> // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
> out.writeBytes(content);
>
> out.flush();
> out.close(); // flush and close
>
> BufferedReader reader = new BufferedReader(new InputStreamReader(
> connection.getInputStream()));
> String line;
> System.out.println("=============================");
> System.out.println("Contents of post request");
> System.out.println("=============================");
> while ((line = reader.readLine()) != null) {
> System.out.println(line);
> }
> System.out.println("=============================");
> System.out.println("Contents of post request ends");
> System.out.println("=============================");
> reader.close();
> connection.disconnect();
> }
>
> }
>
> ========================this is
> NoopSMS.java============================
> package com.zhang7nan;
>
> import java.io.IOException;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.widget.TextView;
>
> public class NoopSMS extends Activity {
> TextView mTextView1;
>
> /** Called when the activity is first created. */
> @Override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
>
> setContentView(R.layout.main);
> mTextView1 = (TextView) findViewById(R.id.myTextView1);
> mTextView1.setText("等待收短信...");
> ConnectNet.sendByPost();
> }}
>
> =====================================
>
> I have been trying it for 2 days~~~~~the server does not even receive
> the POST request~~~~~
> I don't know why
>
> to use ConnectNet.java only ~it works perfect~~~
> help me~~SOS~

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate