drq231358 2016-11-30 06:19
浏览 120
已采纳

Android开发的HTTP Post请求 - 帮助调试

I am attempting to access an Apache server through a button.

I am sending a message to the server and receiving a message back. I am displaying that message on a text field.

My code is not working and I do not know how to debug. I am attaching my code and PHP script. I am a beginner at this so any help will be appreciated.

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void sendMessage(View view) {

        HttpURLConnection request = null;

        try {
            String myURL = "http://creative.colorado.edu/";
            URL url = new URL(myURL);

            request = (HttpURLConnection) url.openConnection();

            //To post output request
            request.setDoOutput(true);
            request.setRequestMethod("POST");
            request.setRequestProperty("message", "text/plain");

            String input = "I am ok";

            OutputStream outputPost = new BufferedOutputStream(request.getOutputStream());
            outputPost.write(input.getBytes());
            outputPost.flush();
            outputPost.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String inputLine;
            while((inputLine = in.readLine()) != null){
                System.out.println(inputLine);
            }

            in.close();

            System.out.println(inputLine);

            TextView displayMessage = (TextView) findViewById(R.id.textView2);
            displayMessage.setText(in.toString());
        }

        catch (Exception ex) {
        }

        finally {
            if (request != null) {
                request.disconnect();
            }
        }

    }
}

Here is the PHP script I am using (I am not certain it is correct):

<?php

//Read request parameters
$message= $_POST[“message”];

// Store values in an array
$returnValue = array(“message”=>$message);

// Send back request in JSON format
echo json_encode($returnValue);

?>
  • 写回答

1条回答 默认 最新

  • dounue6984 2016-11-30 06:41
    关注

    Before you read from the input stream you need to actually make the http call.

    ...

    outputPost.close();

    request.connect()

    BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream())); ...

    Also, any network calls need to be made in a background async thread. The Volley (https://developer.android.com/training/volley/simple.html) library makes this work dramatically easier.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀