1

我可以像这样在浏览器上发送:

浏览器网址: http://192.168.xx:8080/HttpGetServlet/HelloServlet?jsoncuk= {"id":"102","email":"[hozka@sim.com.tr]","phone":"[ ]","name":"hoz"}&number=100

和我的回应:

{"id":"102","email":"[hozka@sim.com.tr]","phone":"[]","name":"hoz"}

但是当我在 android 上尝试它时,它给出了非法ArgumentException

我的安卓代码:

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");
        }

}

我的 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");
    }



}

我的日志猫

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)
4

2 回答 2

0

请尝试编码您的网址,

url= java.net.URLEncoder.encode(url);
于 2013-07-12T00:35:04.377 回答
0

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

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

相反,您可以调用:

task.execute(URL);

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

你应该编码str

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

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

于 2013-07-12T01:00:50.810 回答