0

「メソッドexecuteMethod(GetMethod)は、タイプHttpClientに対して未定義です」というエラーが表示されます。以下は私のコードです。

HttpClient client = new DefaultHttpClient();
GetMethod get = new GetMethod("http://10.0.2.2:8080/GnP22/GetNpostServlet?TicketNo="+etTkt.getText()); 

try { 
                     int status = client.executeMethod(get); 
                     //TextView 3
                     //resultado=(TextView)findViewById(R.id.ws_response); 
                     String res=""; 
                     if(status!=404) 
                             res=get.getResponseBodyAsString(); 
                     else 
                             //res=getString(R.string.ws_not_found); 
                     tvDescription.setText(res); 
             }
                 catch (Exception e) { 
                     Log.e("Error:",e.getMessage()); 
             } 
             finally { 
                     get.releaseConnection(); 
                     get=null; 
             }

ライブラリとして httpclient3.1 jar を添付しました。

4

1 に答える 1

0
HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet("http://10.0.2.2:8080/GnP22/GetNpostServlet?TicketNo="+etTkt.getText()); 

    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
        // Examine the response status
        Log.i("Response",response.getStatusLine().toString());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release

        if (entity != null) {


            InputStream instream = entity.getContent();
            String result= convertStreamToString(instream);
            // now you have the string representation of the HTML request
            instream.close();
        }


    } catch (Exception e) {}
}

    private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
于 2013-08-14T07:45:39.653 に答える