2011-01-11 134 views
1

我無法從我的jsp頁面中獲取android傳遞的值。我犯了錯誤。任何建議!將參數從android傳遞給servlet/jsp

下面

是我的代碼:

==========================

客戶端:Android的活動

package org.test2; 

import java.io.IOException; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class test2Activity extends Activity { 
    EditText ed; 
    Button ok; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ed=(EditText)findViewById(R.id.EditText01); 
     ok=(Button)findViewById(R.id.Button01); 
     ok.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       // Create a new HttpClient and Post Header 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpGet request=new HttpGet("http://localhost:8080/t5/jsp1.jsp?name=hiren"); 
       //HttpPost httppost = new HttpPost("http://localhost:8080/t5/jsp1.jsp?name=hiren"); 
       try {    
        // Execute HTTP Post Request 
        HttpResponse response = httpclient.execute(request);      

       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
       }catch (IOException e) { 
        // TODO Auto-generated catch block 
       } 
      } 
     }); 
    } 

} 

=======================================

服務器:JSP

<%@ page import="java.util.*" language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<script type="text/javascript"> 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
Parameter value = 
<%=request.getParameter("name") %> 
</body> 
</html> 

=========================================

+0

爲什麼不在JSP中打印什麼?你怎麼知道你是否從Android獲取了某些東西! – 2011-01-11 09:38:04

回答

1

兩個主要問題這裏根據我:

您正在使用本地主機,我想你正在嘗試使用模擬器,而不是本地主機,你必須分配你的機器的IP,如果你有DHCP然後在Android本地主機應該這樣做因此,嘗試

"http://10.0.2.2:8080/myapp/servletname?key=value"

您試圖在瀏覽器中進行打印,而不是我的建議是嘗試打印在控制檯中的價值它會告訴你,然後你可以將它保存到數據庫或者做這個過程。

希望這可以解決問題或任何開發人員面臨這樣的問題。