Есть php скрипт на сервере
<?php
$response = array();
if (isset($_POST['NameFile']) && isset($_POST['PathFile']) && isset($_POST['Tag']) && isset($_POST['Rating']) && isset($_POST['Author']) && isset($_POST['ArrayEditor']) && isset($_POST['ArrayAnswer']))
{
$NameFile = $_POST['NameFile'];
$PathFile = $_POST['PathFile'];
$Tag = $_POST['Tag'];
$Rating = $_POST['Rating'];
$Author = $_POST['Author'];
$ArrayEditor = $_POST['ArrayEditor'];
$ArrayAnswer = $_POST['ArrayAnswer'];
require 'db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("INSERT INTO file(NameFile, PathFile, Tag, Rating, Author, ArrayEditor, ArrayAnswer) VALUES('$NameFile', '$PathFile', '$Tag', '$Rating', '$Author', '$ArrayEditor', '$ArrayAnswer')");
if ($result)
{
$response["success"] = 1;
$response["message"] = "Product successfully created.";
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
}
else
{
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
Если сейчас зайти просто по адресу http://educt.landmarkstd.ru/create_file.php
То он выведет массив с ошибкой.
Теперь пытаюсь в android создать новую запись
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("NameFile", itemList.nameList));
params.add(new BasicNameValuePair("PathFile", "serverFalse"));
params.add(new BasicNameValuePair("Tag", itemList.Tag));
params.add(new BasicNameValuePair("Rating", "5.0"));
params.add(new BasicNameValuePair("Author", "Fasd"));
params.add(new BasicNameValuePair("ArrayEditor", "Test"));
params.add(new BasicNameValuePair("ArrayAnswer", "2*2:2*3:2*4"));
final 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)
{
Log.d("myLog", "Файл успешно добавлен");
}
}
catch (JSONException e)
{
Log.d("myLog", "Неудалось");
e.printStackTrace();
}
return null;
И эта строчка Log.d("Create Response", json.toString());
Возвращает null, хотя в самой бд запись добавляется.
А это Лог ошибок:
07-21 13:13:12.558 27177-27216/ru.landmarkstd.educt E/JSON Parser: Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
07-21 13:13:12.569 27177-27216/ru.landmarkstd.educt E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: ru.landmarkstd.educt, PID: 27177
Theme: themes:{default=overlay:com.baranovgroup.nstyle, iconPack:com.baranovgroup.nstyle, fontPkg:com.baranovgroup.nstyle, com.android.systemui=overlay:com.baranovgroup.nstyle, com.android.systemui.navbar=overlay:com.baranovgroup.nstyle}
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
at ru.landmarkstd.educt.FunctionalClass.Server$CreateNewProduct.doInBackground(Server.java:70)
at ru.landmarkstd.educt.FunctionalClass.Server$CreateNewProduct.doInBackground(Server.java:42)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
А это jsonParser
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.util.List;
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 method
public JSONObject makeHttpRequest(String url, String method,
List 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;
}
}