2013-01-10 101 views
1

我想從一個android應用程序發送一個字符串到一個servlet,然後檢索到我的android應用程序的字符串,但是當我嘗試調用該servlet它強制關閉我 和我不知道爲什麼(IM很新的Android,這是一個實踐鍛鍊對我來說) 這裏是我的android簡單的應用程序:發送一個字符串從android的

ANDROID

package com.theopentutorials.android; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 
import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
public class HttpGetServletActivity extends Activity implements OnClickListener { 
Button button; 
TextView outputText; 
public static String request = "kjo ishte e gjitha"; 
public static final String URL = ("http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet?param1=" + request); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findViewsById(); 
    button.setOnClickListener(this); 
} 
private void findViewsById() { 
    button = (Button) findViewById(R.id.button); 
    outputText = (TextView) findViewById(R.id.outputTxt); 
} 
public void onClick(View view) { 
    GetXMLTask task = new GetXMLTask(); 
    task.execute(new String[] { URL }); 
} 
private class GetXMLTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 
     String output = null; 
     for (String url : urls) { 
      output = getOutputFromUrl(url); 
     } 
     return output; 
    } 
    private String getOutputFromUrl(String url) { 
     StringBuffer output = new StringBuffer(""); 
     try { 
      InputStream stream = getHttpConnection(url); 
      BufferedReader buffer = new BufferedReader(
        new InputStreamReader(stream)); 
      String s = ""; 
      while ((s = buffer.readLine()) != null) 
       output.append(s); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     return output.toString(); 
    } 

    private InputStream getHttpConnection(String urlString) 
      throws IOException { 
     InputStream stream = null; 
     URL url = new URL(urlString); 
     URLConnection connection = url.openConnection(); 
     try { 
      HttpURLConnection httpConnection = (HttpURLConnection) connection; 
      httpConnection.setRequestMethod("GET"); 
      httpConnection.connect(); 

      if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       stream = httpConnection.getInputStream(); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return stream; 
    } 
    @Override 
    protected void onPostExecute(String output) { 
     outputText.setText(output); 
    } 
} 
} 

,這裏是我的簡單的servlet

SERVLET

import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
@WebServlet("/HelloWorldServlet") 
public class HelloWorldServlet extends HttpServlet { 
private static final long serialVersionUID = 1L; 

public HelloWorldServlet() { 
    super(); 
} 
protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) 
     throws ServletException, IOException { 
    String par1 = request.getParameter("param1"); 
    PrintWriter out = response.getWriter(); 
    out.println(par1); 
} 
} 

而且logcat的日誌錯誤說

01-10 13:36:50.014: E/AndroidRuntime(1187): FATAL EXCEPTION: AsyncTask #1 
01-10 13:36:50.014: E/AndroidRuntime(1187): java.lang.RuntimeException: An error occured while executing doInBackground() 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at android.os.AsyncTask$3.done(AsyncTask.java:299) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.lang.Thread.run(Thread.java:856) 
01-10 13:36:50.014: E/AndroidRuntime(1187): Caused by: java.lang.NullPointerException: lock == null 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.io.Reader.<init>(Reader.java:64) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.io.InputStreamReader.<init>(InputStreamReader.java:122) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.io.InputStreamReader.<init>(InputStreamReader.java:59) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at com.theopentutorials.android.HttpGetServletActivity$GetXMLTask.getOutputFromUrl(HttpGetServletActivity.java:64) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at com.theopentutorials.android.HttpGetServletActivity$GetXMLTask.doInBackground(HttpGetServletActivity.java:54) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at com.theopentutorials.android.HttpGetServletActivity$GetXMLTask.doInBackground(HttpGetServletActivity.java:1) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
01-10 13:36:50.014: E/AndroidRuntime(1187):  ... 4 more 

行的也有人有什麼想法? 非常感謝您的幫助! 祝您有美好的一天!

+0

發佈您的logcat可以是有用 – ben75

+0

謝謝您的回覆,我剛剛發佈的logcat的,但我不能充分理解它 –

回答

2

您需要添加的互聯網訪問權限

<uses-permission android:name="android.permission.INTERNET"/> 

我發現,你應該使用類URLEncoder來編碼URL,因爲您的網址包含空格。請檢查http://developer.android.com/reference/java/net/URLEncoder.html

+0

我已經有了,我試圖直接從servlet發送servlet響應像一個字符串,它的工作.. –

+0

我想通了它是由param1值的空間造成的。嘗試轉義它像kjoi%20shtegjitha – Kai

+0

我曾嘗試發送它就像一個單詞像'公共靜態最終字符串URL = (「http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet?param1=CkemiBota」); '所以不需要urlEncoder,但沒有什麼,再次同樣的錯誤:/ –

1

你忘了從android應用程序中設置param1。

connection.setRequestProperty("param1", "Your String Value"); 

然後你會從Servlet中得到迴應的值。

+0

sure.go上.. @ p3rand0r –

+0

謝謝您的答覆,但我還有2問題,我不知道我必須把這行代碼,在這之後我也想編輯'public static final String URL =(「http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet?param1=」 + request);'to'public static final String URL =(「http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet」);'對嗎?另外我發佈了logcat,如果這是爲了任何幫助。 –

+0

作爲你的setRequestProperty,那麼你會得到param1值到你的servlet,因此你也會得到一個response.just設置requestproperty線,你完成了。保持所有其他線路。 –

相關問題