Tom Xue
Tom Xue

Reputation: 3355

How to make a web server which could communicate with my Android phone?

package com.yarin.android.Examples_08_01;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

//以Get方式上传参数
public class Activity03 extends Activity {
    private final String DEBUG_TAG = "Activity03";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.http);
        TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);
        // http address "?par=abcdefg" is the argument to be posted
        String httpUrl = "http://192.168.0.100:8080/httpGet.jsp?par=test";
        // 获得的数据
        String resultData = "";
        URL url = null;
        try {
            // 构造一个URL对象
            url = new URL(httpUrl);
        } catch (MalformedURLException e) {
            Log.e(DEBUG_TAG, "MalformedURLException");
        }
        if (url != null) {
            try {
                // 使用HttpURLConnection打开连接
                HttpURLConnection urlConn = (HttpURLConnection) url
                        .openConnection();
                // 得到读取的内容(流)
                InputStreamReader in = new InputStreamReader(
                        urlConn.getInputStream());
                // 为输出创建BufferedReader
                BufferedReader buffer = new BufferedReader(in);
                String inputLine = null;
                // 使用循环来读取获得的数据
                while (((inputLine = buffer.readLine()) != null)) {
                    // 我们在每一行后面加上一个"\n"来换行
                    resultData += inputLine + "\n";
                }
                // 关闭InputStreamReader
                in.close();
                // 关闭http连接
                urlConn.disconnect();
                // 设置显示取得的内容
                if (resultData != null) {
                    mTextView.setText(resultData);
                } else {
                    mTextView.setText("读取的内容为NULL");
                }
            } catch (IOException e) {
                Log.e(DEBUG_TAG, "IOException");
            }
        } else {
            Log.e(DEBUG_TAG, "Url NULL");
        }
        Button button_Back = (Button) findViewById(R.id.Button_Back);
        /* 监听button的事件信息 */
        button_Back.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                /* 新建一个Intent对象 */
                Intent intent = new Intent();
                /* 指定intent要启动的类 */
                intent.setClass(Activity03.this, Activity01.class);
                /* 启动一个新的Activity */
                startActivity(intent);
                /* 关闭当前的Activity */
                Activity03.this.finish();
            }
        });
    }
}

For the above code, I understand how it works. It runs as an application and needs to communicate with a web server.

But I don't know how to make a web server which could be a container of "http://192.168.0.100:8080/httpGet.jsp".

I did some investigation.

(1) On Android phone, i-jetty, kws, atieews may help, but I failed to make them work for my purpose.

(2) On PC, tomcat is a good candidate to be as jsp container. But it provides localhost:8080 address, that means only application runs on PC could communicate with it. Am I right? How to make my Android phone to connect tomcat (runs on my PC)?

(3) Any other idea?

Thanks!

Upvotes: 2

Views: 915

Answers (1)

Milos Cuculovic
Milos Cuculovic

Reputation: 20223

To make a server which can communicate with your android app you can use SOAP services or JSON. Those two are the most used ones (JSON is faster and in my opinion better to use but this can be discussed).

Take a look on some tutorials on how to create the server side app for your android app. This is not an easy taski if you newer set up a server.

Upvotes: 2

Related Questions