Sto sviluppando un'applicazione Android che invia e riceve dati da un server python ospitato in pythonanywhere utilizzando le richieste HTTP e JSON.Invia richiesta POST su dati mobili
L'applicazione funziona perfettamente via Wi-Fi ma il problema si verifica quando lo utilizzo tramite Mobile Data. Tutti i dati provenienti dal server con richieste GET funzionano perfettamente ma le richieste POST e DELETE non sembrano inviare o funzionare in altro modo.
non so se il problema è
- server gratuito
- non attendibile app
- permessi
PostRequest.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public abstract class PostRequest extends AsyncTask<String,Void,String> {
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;
}
@Override
protected String doInBackground(String... params) {
InputStream inputstream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
StringEntity se = new StringEntity(params[1]);
post.setEntity(se);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(post);
inputstream = httpResponse.getEntity().getContent();
if(inputstream != null)
result = convertInputStreamToString(inputstream);
else
result = "Did not work!";
} catch (Exception e) {
Log.i("[ error stream ]", e.toString());
}
return result;
}
@Override
abstract public void onPostExecute(String result);
}
RequestAddNewStory.java
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.Toast;
import com.shortstory.activities.AddHashtag;
import com.shortstory.api.PostRequest;
public class RequestAddNewStory extends PostRequest {
Context context ;
Button AddHashtag ;
public RequestAddNewStory(Context context , Button AddHashtag)
{
this.context = context ;
this.AddHashtag = AddHashtag ;
}
@Override
public void onPostExecute(String result) {
JSONObject json;
try {
json = new JSONObject(result);
String s = json.getString("message");
if (s.equals("done")) {
Toast.makeText(context,"now add hashtags", Toast.LENGTH_LONG).show();
Intent Addhashtag = new Intent(context , AddHashtag.class);
context.startActivity(Addhashtag);
((Activity) context).finish();
}else
{
AddHashtag.setEnabled(true);
Toast.makeText(context,"can't add story", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
quando inviare richiesta
json.accumulate("user_name", "mkm");
json.accumulate("title", "hello");
json.accumulate("body", "hello all");
json.accumulate("img", " ");
RequestAddNewStory rans = new RequestAddNewStory(this , AddHashtag);
rans.execute("http://-----.pythonanywhere.com/api/story" , json.toString());
dati mobili possono essere lento in modo da poter gestire con 'timeout' collegamento, per ulteriori informazioni vedere il codice ... – hrskrs
grazie, mi post po 'di codice ora –
vi posto qualche codice ora @hrskrs –