如何使用get方法将JSON从Android发送到Java Servlet

时间:2013-07-11 23:10:40

标签: android json servlets get

我可以在webbrowser上发送:

浏览器网址: http://192.168.x.x:8080/HttpGetServlet/HelloServlet?jsoncuk= {" ID":" 102""电子邮件":" [hozka@sim.com.tr]" "电话":" []""名称":"奥斯"}&安培;数= 100

和我的回复:

{" ID":" 102""电子邮件":" [hozka@sim.com.tr]",& #34;电话":" []""名称":"奥斯"}

但是当我在android上尝试它时会发出illegalArgumentException

我的Android代码:

public class MainActivity extends Activity implements OnClickListener {

public static String str = "{\"id\":\"101\",\"email\":\"[hozka@ias.com.tr]\",\"phone\":\"[]\",\"name\":\"hoz\"}";
public static String URL = "http://192.168.x.x:8080/HttpGetServlet/HelloServlet" + "?jsoncuk=" + str + "&number=100";

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) {
            String output = null;
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                output = EntityUtils.toString(httpEntity);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return output;
        }

        @Override
        protected void onPostExecute(String output) {
            //outputText.setText(output + "selam");
            servlet_donus = output;
            outputText.setText(servlet_donus + "selam");
        }

}

My Java Servlet代码:

/**
 * Servlet implementation class HelloServlet
 */
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        Integer numb = Integer.parseInt(request.getParameter("number"));
        if(numb==300 || numb == 200 || numb == 100){
            out.println(request.getParameter("jsoncuk"));
            out.flush();
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        Integer numb = Integer.parseInt(request.getParameter("number"));
        if(numb==300 || numb == 200 || numb == 100){
            out.println(request.getParameter("jsoncuk"));
            out.flush();
        }
        else
            out.println("ses");
    }



}

我的Logcat

07-12 15:03:26.100: E/AndroidRuntime(1873): FATAL EXCEPTION: AsyncTask #1
07-12 15:03:26.100: E/AndroidRuntime(1873): java.lang.RuntimeException: An error occured while executing doInBackground()
07-12 15:03:26.100: E/AndroidRuntime(1873):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.lang.Thread.run(Thread.java:856)
07-12 15:03:26.100: E/AndroidRuntime(1873): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 63: http://192.168.127.50:8080/HttpGetServlet/HelloServlet?jsoncuk={"id":"101","email":"[hozkara@ias.com.tr]","phone":"[]","name":"hozkara@ias.com.tr"}&number=100
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.net.URI.create(URI.java:727)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at com.example.ssstart.MainActivity$GetXMLTask.getOutputFromUrl(MainActivity.java:66)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at com.example.ssstart.MainActivity$GetXMLTask.doInBackground(MainActivity.java:57)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at com.example.ssstart.MainActivity$GetXMLTask.doInBackground(MainActivity.java:1)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-12 15:03:26.100: E/AndroidRuntime(1873):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

2 个答案:

答案 0 :(得分:0)

请尝试对您的网址进行编码,

url= java.net.URLEncoder.encode(url);

答案 1 :(得分:0)

您不需要在此处为​​您的URL创建String [],因为AsyncTask的execute()接受var args:

task.execute(new String[] { URL });

相反,您可以致电:

task.execute(URL);

没有堆栈跟踪,这只是猜测。但它可能会解释它,因为你最终得到一个单项数组,其中包含一个String []。我希望这是有道理的。

你应该编码str

str = java.net.URLEncoder.encode(str);

我假设您使用的是与打印不同的IP地址(替换了x.x)。