Android returns an empty HTTP string

I have a problem. I have a class AsyncTaskActivityin which there is a method downloadUrlthat communicates with the php server through HttpURLConnection. The php server that communicates with MySql DB should return, depending on the type of item, string text or string in json format. HttpURLConnectionreturns an empty string when the type of the item returned by the server is string text. This is a class AsyncTaskActivity.

public class AsyncTaskActivity extends AsyncTask<String, Void, String> {
private Context context;
String serverParameters;
public AsyncResponse delegate = null;
String url = null;
private ProgressDialog pd;

public AsyncTask(Context c, String s, AsyncResponse<String> delegate) {
    this.serverParameters = s;
    this.context = c;
    this.delegate = delegate;
}

@Override
protected String doInBackground(String... strings) {
    try {
        url = downloadUrl(strings[0]);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return url;
}

protected void onPreExecute() {
    super.onPreExecute();
    pd = new ProgressDialog(context);
    pd.setTitle("Please Wait...");
    pd.setMessage("Loading...");
    pd.show();
}

protected void onPostExecute(String result) {
    super.onPostExecute(result);
    System.out.println("RESULT = " + result);
    delegate.processFinish(result);
    pd.dismiss();
}

private String downloadUrl(String targetURL) throws IOException {
    URL url;
    HttpURLConnection connection = null;
    try {
        // Create connection
        url = new URL(targetURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // Send request
        OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
        wr.write(serverParameters);
        wr.flush();
        wr.close();

        // Get Response
        InputStream is = connection.getInputStream();
        String data = convertStreamToString(is);
        is.close();
        return data;

    } catch (Exception e) {
        e.printStackTrace();
        return null;

    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

private String convertStreamToString(InputStream inputStream) {
    Scanner s = new Scanner(inputStream).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

AvailabilityActivity. , , (, , , ) json . , (type element = "expert", access = "read", json to write = ""), . "expert" . Riepilogo.setOnClickListener serverParameters , sendRequest() AsyncTaskActivity().

public class AvailabilityActivity extends ActionBarActivity implements AsyncResponse<String> {
private Spinner spinner;
private Spinner spinner1;

/* Parameters Expertise*/

private static final String TIPO_ELEMENTO = "expertise";
private static final String ACCESSO = "read";
private AsyncTaskActivity send_request;
private String serverParameters;
private JSONObject jsonObject;
private ArrayList<String> list = new ArrayList<String>();
AvailabilityEntity availabilityEntity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_availability);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner = (Spinner) findViewById(R.id.spinner2);

    Riepilogo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            date_ = date.getText().toString();
            startTime_ = startTime.getText().toString();
            endTime_ = endTime.getText().toString();
            timeEditText_ = timeEditText.getText().toString();
            availabilityEntity = new AvailabilityEntity(date_, startTime_, endTime_, itemSpinner, timeEditText_);
            showSummary();
        }
    });

    serverParameters = generateParameters(TIPO_ELEMENTO, ACCESSO, "");
    sendRequest();
}

private String generateParameters(String tipoElementoLogin, String accesso, String s) {
    String parameters = "accesso:" + accesso + ", elemento:" + tipoElementoLogin + ", jsonDaScrivere:" + s;
    return parameters;
}

private void sendRequest() {
    send_request = new AsyncTaskActivityString(this, serverParameters, this);
    send_request.execute(getApplicationContext().getResources().getString(R.string.serverQuery));
}

@Override
public void processFinish(String output) {
    System.out.println("Value: " + output);
    // Aggiunge lista expertise dal server in spinner
    list.add(output);
    ArrayAdapter<String> array_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
    array_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(array_adapter);

}
+4
1

, connection.connect(). :

private String downloadUrl(String targetURL) throws IOException {
   ...
   ...

   // Get Response
   connection.connect();
   int http_code = connection.getResponseCode();

   String data = null;
   if (http_code == HttpURLConnection.HTTP_OK) {
      data = this.convertStreamToString(connection.getInputStream());
   }
0

All Articles