Erreur AsyncTask requête POST JSON
Bonjour,
j'ai un problème lorsque je lance ma classe qui effectue une requete POST, après avoir fait des rechercher malheureusement je n'ai rien trouvé.
je vous donne l'erreur :
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Et le code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| public class MyAsyncTaskPostIdNum extends AsyncTask<String, Void, String> {
InputStream inputStream = null;
String result = "";
String adresseUrlPost;
String responseBody;
//MainActivity objParent = new MainActivity();
HttpResponse response;
String content;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0], params[1]);
return null;
}
protected void onPostExecute() {
// Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}
public String postData(String telephoneP, String mdpP) {
try {
//Composition de l'adresse url avec la valeur de l'edit text et du num de téléphone récupéré
adresseUrlPost="http://"+MainActivity.valUrl+"/PersonSurveille";
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(adresseUrlPost);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("telephone", telephoneP);
jsonObject.accumulate("password", mdpP);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "probleme, ne fonctionne pas";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
} |