Hola,
necesito poder consumir un servicio web desde android, tengo uno de prueba hecho en un simple archivo java y funciona pero desde android no funciona le puse permisos de internet etc.. ahora explico.
Este es el archivo java que funciona, si lo ejecuto va a buscar
Código Java:
Ver originalpackage validadouble;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
URL url
= new URL("http://10.201.1.114:8000/api/profile");
String basicAuth
= "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImZkYjIzYWUyZDNkN2JkMTlmZjBmMTA1NmM5Mzg5NWE3MzQ4N2EzNzdkNDNmYjdhNWQ4Yjg2YTYzYTcwNjY4YWM5MWM2OGNjMzhhNjlhOTY0In0.eyJhdWQiOiIzIiwianRpIjoiZmRiMjNhZTJkM2Q3YmQxOWZmMGYxMDU2YzkzODk1YTczNDg3YTM3N2Q0M2ZiN2E1ZDhiODZhNjNhNzA2NjhhYzkxYzY4Y2MzOGE2OWE5NjQiLCJpYXQiOjE1NDIwMzY2MTEsIm5iZiI6MTU0MjAzNjYxMSwiZXhwIjoxNTczNTcyNjExLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.iQoKJu6bzzqbfXI8YvCsObFaA7MsufHtaRCK3JS3arVq2HWvDP268QxG_NzCACVxnuuvYkHXbtClRzkqYfvcWP_SUhd2jTWUjek5OPueUg7XEu-fXyEbu3LXfK4jL2u2w1w_s8a4MfEJIbEJuxBefMa9u8LkK3HuSBPoLzsxYnhONtXFm_qrAsUKnUQgmYq0cKm-4prO5PzNF1FACgIhfmwdOeuNi66ICajjI9eUxF17nT4Rbn-s4_xt4B81sQ_Mxsxejafz2NS4a1zuNDUzkvpWKb34EhKW5r9Pk4SApYlzfkgarQKqUNKw_UnkZPAOf_oVmboGKi1dtsxX3-Oic16hAckR9s_h5P9A6E1xRa8BoIKd8FZhQtSy4vCcsSbyk3jxua_NR-AnydHRgbM_WujDWLM_eItZrxrembXySKsb4dSCMm6pOqvM6i6JNyIUh5LWrP0M1UrvzfeqWSgF4DFV8GhHbuQ6HOGKlC1lLEEYcPgAiWFRW9d-603Lhc8xp96GA7UrdvbqMtb6dUFHbv5Uhy_Ac0O_-3lzu9X-8Kcqo8sICsTuaiwCQ0274dVTkjzLNYTa5TSsLkp-VnWCjelDq-uE9Y10_JbUhuihZZ-oKnQJZ5bIxJPSZ5sj1BzPCMO031SwAGI7A1eXduD-nLZqSAhkiZQS6FWH6WWCtYE";
urlConnection.setRequestProperty ("Authorization", basicAuth);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
try {
while((line = reader.readLine()) != null){
}
} finally {
urlConnection.disconnect();
}
}
}
Lo que no funciona es lo que tengo en android, pondre el archivo MainActivity.java, ClassConnection.java y el AndroidManifest.xml
MainActivity.java
Código Java:
Ver originalpackage com.example.practice.getpost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
TextView txtnombre;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtnombre = (TextView) findViewById(R.id.txtnombre);
ClassConnection connection = new ClassConnection();
try {
String response
= connection.
execute("http://10.201.1.114:8000/api/profile").
get();
JSONArray jsonArray = new JSONArray(response);
//JSONObject jsonObject = jsonArray.getJSONObject(2);
//String name = jsonObject.getString("name");
//String name = jsonArray.getJSONObject(2).toString();
//String nombres = "";
//ArrayList<String> listado = new ArrayList<>();
//for (int i= 0; i < jsonArray.length(); i++){
//JSONObject json = jsonArray.getJSONObject(i);
//listado.add(json.getString("name"));
//nombres += json;
//}
txtnombre.setText(jsonArray.toString());
} catch (ExecutionException e) {
e.printStackTrace();
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
ClassConnection.java
Código Java:
Ver originalpackage com.example.practice.getpost;
import android.os.AsyncTask;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ClassConnection
extends AsyncTask
<String,
String, String
> { @Override
try {
url
= new URL(strings
[0]); e.printStackTrace();
}
try {
httpURLConnection.connect();
String basicAuth
= "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImZkYjIzYWUyZDNkN2JkMTlmZjBmMTA1NmM5Mzg5NWE3MzQ4N2EzNzdkNDNmYjdhNWQ4Yjg2YTYzYTcwNjY4YWM5MWM2OGNjMzhhNjlhOTY0In0.eyJhdWQiOiIzIiwianRpIjoiZmRiMjNhZTJkM2Q3YmQxOWZmMGYxMDU2YzkzODk1YTczNDg3YTM3N2Q0M2ZiN2E1ZDhiODZhNjNhNzA2NjhhYzkxYzY4Y2MzOGE2OWE5NjQiLCJpYXQiOjE1NDIwMzY2MTEsIm5iZiI6MTU0MjAzNjYxMSwiZXhwIjoxNTczNTcyNjExLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.iQoKJu6bzzqbfXI8YvCsObFaA7MsufHtaRCK3JS3arVq2HWvDP268QxG_NzCACVxnuuvYkHXbtClRzkqYfvcWP_SUhd2jTWUjek5OPueUg7XEu-fXyEbu3LXfK4jL2u2w1w_s8a4MfEJIbEJuxBefMa9u8LkK3HuSBPoLzsxYnhONtXFm_qrAsUKnUQgmYq0cKm-4prO5PzNF1FACgIhfmwdOeuNi66ICajjI9eUxF17nT4Rbn-s4_xt4B81sQ_Mxsxejafz2NS4a1zuNDUzkvpWKb34EhKW5r9Pk4SApYlzfkgarQKqUNKw_UnkZPAOf_oVmboGKi1dtsxX3-Oic16hAckR9s_h5P9A6E1xRa8BoIKd8FZhQtSy4vCcsSbyk3jxua_NR-AnydHRgbM_WujDWLM_eItZrxrembXySKsb4dSCMm6pOqvM6i6JNyIUh5LWrP0M1UrvzfeqWSgF4DFV8GhHbuQ6HOGKlC1lLEEYcPgAiWFRW9d-603Lhc8xp96GA7UrdvbqMtb6dUFHbv5Uhy_Ac0O_-3lzu9X-8Kcqo8sICsTuaiwCQ0274dVTkjzLNYTa5TSsLkp-VnWCjelDq-uE9Y10_JbUhuihZZ-oKnQJZ5bIxJPSZ5sj1BzPCMO031SwAGI7A1eXduD-nLZqSAhkiZQS6FWH6WWCtYE";
httpURLConnection.setRequestProperty ("Authorization", basicAuth);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
//int code = httpURLConnection.getResponseCode();
//if(code == HttpURLConnection.HTTP_OK){
while((line = reader.readLine()) != null){
buffer.append(line);
}
return buffer.toString();
//}
e.printStackTrace();
}
return null;
}
}
AndroidManifest.xml
Código XML:
Ver original<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.practice.getpost">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
ojala me puedan ayudar,
bye