2012-01-31 28 views
-1

我正在嘗試創建一個與servlet有連接的應用程序。調試器不顯示任何錯誤,無論是servlet有任何錯誤,但仍然代碼不工作 這是Android應用程序My_appActivity.java如何從android應用程序連接到servlet?

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); 
    }  
} 
+2

請定義「不工作」。你是否看到拋出的異常,你是否看到了你所期望的所有服務器端和客戶端的日誌記錄? – 2012-01-31 15:43:09

+0

我在客戶端(通過android應用程序)沒有任何問題,而在服務器端,servlet在以下位置提供EOFException:int len = dis.readInt(); – Mandeep 2012-02-02 04:36:09

回答

0

一旦問題肯定是POST中的Content-Type標頭。您正在告訴servlet您正在發送URL編碼表單,然後繼續將原始字節數據放入請求主體。另外,你實際上不需要在內容中寫出內容的長度 - 爲什麼不把它寫在Content-Length頭部呢?爲什麼不更好地利用內置到HTTP協議中的內容?

+0

我已經改變了Run()方法,加入:conn.setFixedLengthStreamingMode(keyword.getBytes()。length);並且我也省略了內容類型頭文件,但仍然通過servlet獲得相同的EOFException – Mandeep 2012-02-03 07:37:52

+0

您必須*提供內容類型頭文件,並且必須使用與您發送的內容類型實際匹配的頭文件。 – 2012-02-03 13:47:12

相關問題