如何从Android应用程序连接到servlet?

时间:2012-01-31 15:37:12

标签: android

我正在尝试创建一个与servlet连接的应用程序。调试器没有显示任何错误,并且servlet都没有任何错误,但代码仍无法正常工作 这是android应用程序My_appActivity.java

package com.m_app.first_app;

import java.io.DataInputStream; 
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.m_app.first_app.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class My_appActivity extends Activity{
    public static final String TAG = "SearchRecord";
    private EditText mEditText1;
    private Button mButton1;

    @Override
    public void onCreate(Bundle icicle) 
    {
        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(icicle);
        setContentView(R.layout.main);

     // Obtain handles to UI objects
        mEditText1 = (EditText) findViewById(R.id.editText1);
        mButton1 = (Button) findViewById(R.id.button1);


     // Register handler for UI elements
        mButton1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                Log.d(TAG, "mButton1 clicked");
                String keyword = mEditText1.getText().toString();
                networkthread ob = new networkthread(keyword);
            }           
        });    
    }
}

class networkthread implements Runnable
{
    public static final String TAG = "SearchRecord";
    String keyword;
    public networkthread(String keyword)
    {
        this.keyword=keyword;
        Thread t = new Thread(this);
        t.start();
    }
    public void run()
    {
        Log.v(TAG,"Inside the sub thread"); 
        try 
        {
            Log.v(TAG,"Inside try");
            Log.v(TAG,"Before conn");
            URL url = new URL("http://10.0.2.2:8080/My_project/yahoo");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();           
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Accept", "application/octet-stream");
            conn.connect();
            Log.v(TAG,"After conn");
            OutputStream out = conn.getOutputStream();
            Log.v(TAG,"Before DOS");
            DataOutputStream dos = new DataOutputStream(out);
            Log.v(TAG,"After DOS");
            dos.writeInt(keyword.getBytes().length);
            dos.write(keyword.getBytes(),0,keyword.getBytes().length);
            dos.flush();
            dos.close();
            InputStream is = conn.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            String status = dis.readLine();
            conn = null;
            Log.v(TAG,"Finish try");
        } 
        catch (Exception e)
        {
            Log.v(TAG,"Exception: "+e);
            e.printStackTrace();
        }   
    }  
}

Servlet是:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.*;
import java.net.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.*;
import java.io.*;

public class search extends HttpServlet {
    private InputStream is = null;
    private DataInputStream dis = null;
    PrintWriter out;
    protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException , IOException{
        try{
            out=response.getWriter();
            is = request.getInputStream();
            dis = new DataInputStream(is);
            int len = dis.readInt();
            byte data[] = new byte[len];
            dis.read(data,0,len);
            String Keyword = new String(data);
            out.print("Keyword :"+Keyword);
            is.close();
            dis.close();
            String s= "success";

        } catch(Exception e)
        {
            out.print(e);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        service(request,response);
    }       
}

1 个答案:

答案 0 :(得分:0)

问题肯定是POST中的Content-Type标头。您告诉servlet您正在发送URL编码表单,然后继续将原始字节数据放入请求正文中。

另外,你真的不需要在正文中写出内容的长度 - 为什么不把它写在Content-Length头中呢?为什么不更好地利用HTTP协议内置的内容呢?