Erreur "End of input at character 0 of"
Bonjour,
J'ai réalisé un code pour insérer des données dans un serveur distant depuis un client Android et j'ai bien réussi à insérer des données mais juste après, l'application se bloque et il m'affiche le message d'erreur suivant :
Citation:
End of input at character 0 of
J'ai cherché sur le net mais je n'ai pas trouvé de solution qui convienne.
Voici mon code de la classe JSONParser
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
| public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if(method == "GET"){
//request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
} |
Et voici la classe d'ajout dans la BDD
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 96
| public class AjouterClient extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputid;
EditText inputnom;
EditText inputprix;
EditText inputdesc;
Button btnCreateProduct;
// url create product
private static String url_create_product = "http://192.168.1.16/createPRODUCT2.php";
private static final String TAG_SUCCESS = "success";
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajouterclient);
// Edit Text
inputid = (EditText) findViewById(R.id.btnid);
inputnom = (EditText) findViewById(R.id.btnnom);
inputprix = (EditText) findViewById(R.id.btnprix);
inputdesc = (EditText) findViewById(R.id.btndesc);
btnCreateProduct = (Button) findViewById(R.id.btnvalider);
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CreateNewProduct().execute();
}
});
}
class CreateNewProduct extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AjouterClient.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String id = inputid.getText().toString();
String nom = inputnom.getText().toString();
String prix = inputprix.getText().toString();
String description = inputdesc.getText().toString();
//
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("PID", id));
params.add(new BasicNameValuePair("NAME", nom));
params.add(new BasicNameValuePair("PRICE",prix));
params.add(new BasicNameValuePair("DESCRIPTION", description));
//
//
JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);
//
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// succes created product
Intent i = new Intent(getApplicationContext(), Afficher_listCL.class);
startActivity(i);
finish();
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
} |
Voici également le côté PHP
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
| <?php
$dbName = 'localhost:D:/TEST.FDB';
$dbUser = 'SYSDBA';
$dbPswd = 'masterkey';
// ouvrir la cox a une bdd
@($db = ibase_connect($dbName, $dbUser, $dbPswd)) or $db = nul;
// array for JSON response
$response = array();
// check for required fields
if ( isset($_POST['PID']) && isset($_POST['NAME']) && isset($_POST['PRICE']) && isset($_POST['DESCRIPTION'])) {
$PID = $_POST['PID'];
$NAME = $_POST['NAME'];
$PRICE = $_POST['PRICE'];
$DESCRIPTION = $_POST['DESCRIPTION'];
// mysql inserting a new row
$result = ibase_query("INSERT INTO PRODUCTS( PID, NAME, PRICE, DESCRIPTION) VALUES('$PID','$NAME', '$PRICE', '$DESCRIPTION')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
//echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
// echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
// echo json_encode($response);
}
?> |
Mon logcat m'affiche ce qui suit :
Citation:
29:29.448 745-758/riro15.exemple_web_service E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
03-20 11:29:29.558 745-758/riro15.exemple_web_service W/dalvikvm﹕ threadid=10: thread exiting with uncaught exception (group=0x40015560)
03-20 11:29:30.208 745-758/riro15.exemple_web_service E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NullPointerException
at riro15.exemple_web_service.AjouterClient$CreateNewProduct.doInBackground(AjouterClient.java:92)
at riro15.exemple_web_service.AjouterClient$CreateNewProduct.doInBackground(AjouterClient.java:60)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
************at java.util.concurrent.FutureTask.run(FutureTask.java:138)
************at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
************at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
************at java.lang.Thread.run(Thread.java:1019)
03-20 11:29:36.587 745-745/riro15.exemple_web_service E/WindowManager﹕ Activity riro15.exemple_web_service.AjouterClient has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40538ac8 that was originally added here
android.view.WindowLeaked: Activity riro15.exemple_web_service.AjouterClient has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40538ac8 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:258)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at riro15.exemple_web_service.AjouterClient$CreateNewProduct.onPreExecute(AjouterClient.java:68)
at android.os.AsyncTask.execute(AsyncTask.java:391)
at riro15.exemple_web_service.AjouterClient$1.onClick(AjouterClient.java:56)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Quelqu'un saurait-il m'indiquer à résoudre ce problème ?
Merci d'avance pour votre aide.