0

I am relatively new to Android programming so please bear with me. I am trying to call a php script from an android service using HttpClient. But Eclipse is showing "HttpClient cannot be resolved to a type". The same code is executing when I'm running it in an Activity but it's not working inside a Service.

This is my code

public class sendMessage extends Service {


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String name = (String) intent.getExtras().get("name");
        String message = (String) intent.getExtras().get("message");


        HttpClient client = new DefaultHttpClient();
        try{
        HttpResponse response=client.execute(new HttpGet(url));
        HttpEntity entity=response.getEntity();
        retstr=EntityUtils.toString(entity);
        }
        catch(Exception e){

        }


        return startId;


    };

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}
2
  • Are you missing the import? Commented Jan 20, 2016 at 15:43
  • No I don't know what was wrong exactly. But I restarted my computer and started Eclipse again and now it's resolved Commented Jan 20, 2016 at 15:57

1 Answer 1

1

This is what I use

URL url1;
try {
    url1 = new URL(url);
} catch (MalformedURLException e) {
    throw new IllegalArgumentException("invalid url");
}

String body = "";
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
try {
    conn = (HttpURLConnection) url1.openConnection();
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setFixedLengthStreamingMode(bytes.length);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

    // post the request
    OutputStream out = conn.getOutputStream();
    out.write(bytes);
    out.close();

    // handle the response
    int status = conn.getResponseCode();
    InputStream is = new BufferedInputStream(conn.getInputStream());
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder result = new StringBuilder();
    String line;
    while((line = reader.readLine()) != null) {
        result.append(line);
    }
    String message = result.toString();
    if (status != 200) {

        throw new IOException("Post failed with error code " + status);
    }
}
catch(Exception e){
    e.printStackTrace();
}
finally {
    if (conn != null) {
        conn.disconnect();
    }
}

If you want to pass key value pairs, use the following in the above code:

StringBuilder bodyBuilder = new StringBuilder();
//params is Map<String,String>
Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<String, String> param = iterator.next();
    bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
    if (iterator.hasNext()) {
        bodyBuilder.append('&');
    }
}
String body = "";
byte[] bytes = body.getBytes();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.