HttpResponse returns null

Thursday, July 3, 2014

I want to search a diretory with the following php script and get the displayed JSONArray via android app. But everytime I call the android app the HttpResponse null and i dont know why.


PHP:



`<?php
$pfad = "./months";
$files = scandir($pfad);
$result = array();
for($a = 2; $a < count($files); $a++)
{
$result[] = $files[$a];
}
print json_encode($result);
?>`


If I open the script on my server i get ["5.txt","June2014.txt"].


Android:



import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

JSONArray finalResult;
Integer responseCode;
TextView show;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new MyAsyncTask().execute();

show = (TextView)findViewById(R.id.textView1);
show.setText(String.valueOf(responseCode));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private class MyAsyncTask extends AsyncTask<String, Integer, Integer>{

@Override
protected Integer doInBackground(String... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myurl.php");

try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode();

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;

}


}

}


Note: myurl.php is really the the right server address.


I'd be happy for any help







http://ift.tt/1kgWF7t