在本地主机 AsyncHttpClient 客户端中连接 api

Connect with api in localhost AsyncHttpClient client

我的片段

public class UsuarioFragment extends Fragment {

    JSONObject usuario;
    View v;
    String nombreUsuario;
    String emailUsuario;
    String tipoUsuario;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.activity_usuario_fragment, container, false);

        //Code connection APi localhost...       
        return v;
    }
}

我如何连接到本地主机(我使用 WAMP)API

AsyncHttpClient client = new AsyncHttpClient();
client.setMaxRetriesAndTimeout(0,10000);

String Url ="http://192.168.126.1/apimyteam/public/api/usuario/2";

client.get(this,Url, new AsyncHttpResponseHandler() {
    public void onStart() {
        Snackbar.make(getActivity().findViewById(android.R.id.content), "Descargando usuario...", Snackbar.LENGTH_LONG)
        .show();
    }

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
    //public void onSuccess(String content) {


        JSONObject cliente = null;
        String str = new String(responseBody);

        try {
            cliente = new JSONObject(str);         
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            nombreUsuario=cliente.getString("uNombre");
            emailUsuario=cliente.getString("uEmail");
            tipoUsuario=cliente.getString("uTipo");

            TextView usuario= (TextView) v.findViewById(R.id.textUsuario);
            TextView email= (TextView) v.findViewById(R.id.textEmail);
            TextView tipo= (TextView) v.findViewById(R.id.textTipo);

            usuario.setText(nombreUsuario);
            email.setText(emailUsuario);
            tipo.setText(tipoUsuario);
        } catch (JSONException e) {
            Snackbar.make(getActivity().findViewById(android.R.id.content), "Se ha producido un error con el usuario...", Snackbar.LENGTH_LONG)
            .show();
            return;
        }
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
        // called when response HTTP status is "4XX" (eg. 401, 403, 404)
        String str = new String(error.getMessage().toString());
        String valor = "No se ha podido recuperar los datos desde el servidor. " + str;

        Snackbar.make(getActivity().findViewById(android.R.id.content), valor, Snackbar.LENGTH_LONG)
        .show();
    }
});

错误:

Cannot resolve method 'get(com.example.roberto.androidprototipofinal.UsuarioFragment, java.lang.String, anonymous com.loopj.android.http.AsyncHttpResponseHandle)'

我尝试使用getContext() 或getActivty() 来解决这个问题,但还是不行...

因为你在 Fragment 中,你必须将 this 更改为 getActivity(),这将解决问题

Cannot resolve method 'get(com.example.roberto.androidprototipofinal.UsuarioFragment, java.lang.String, anonymous com.loopj.android.http.AsyncHttpResponseHandle)'

编辑

Okey but now it says: "Permissions Denied"

然后您必须在 manifest.xml

中添加权限
<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET" />
 <application>
 ....
 </application>
</manifest>