4
I had made a webservice in java, now I’m switching to PHP and I’m having difficulty implementing a page that receives via POST a JSON that the Android app sends.
ANDROID
public static String POST(Context context, String endereco, String json){
//Verifica se existe conexão com a internet
if(!existeConexao(context))
return K.FALHA_CONEXAO;
InputStream inputStream = null;
HttpClient httpClient = new DefaultHttpClient();
String result = null;
try{
HttpPost httpPost = new HttpPost(endereco);
httpPost.setEntity(new StringEntity(json, "UTF-8"));
httpPost.setHeader("Accept","application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpClient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream!=null)
result = inputStreamParaString(inputStream);
else
result = K.FALHA;
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
Webservice in JAVA
@Path("/enviarFeedBack/")
@POST
@Produces("application/json ; charset=UTF-8")
public String receberFeedBack(String json){
Feedback fb = new Gson().fromJson(json,Feedback.class);
fb.setData(new Date());
Integer result =
new FeedbackController().inserir(fb);
if(result == null || result<0 )
return K.FALHA;
else
return K.SUCESSO;
}
In short, in PHP do not know the correct way to receive the data via POST that were sent by Android already tried to go through the array $_POST
but I don’t know if that’s right, and I’m also not getting to send that answer back success or failure.
Take a look at this answer: http://answall.com/a/46958/20594
– André Ribeiro
Tries to change the
Content-type
androidapplication/json
forapplication/x-www-form-urlencoded
if all goes well you should receive in the variable$_POST
– Icaro Martins
@Icaromartins tried the way Voce mentioned and it did not work. You have already used this solution successfully?
– Skywalker
@Andréribeiro Oce solved, I had not seen that other topic, so I will put the solution together with the question and you copy and glue as an answer. This is if not closed by doubling the question. It has mt in common with the other. Thank you
– Skywalker
@Juarez Glad you solved it! You can take the PHP code you made, answer your question and mark as accepted. It is very similar to the other but do not believe it is duplicated. Note: in your case there is no need to set these headers.
– André Ribeiro