【问题标题】:I'm trying to call a web api (that i made in ASP.NET) in java (android)我正在尝试在 java(android)中调用一个 web api(我在 ASP.NET 中制作的)
【发布时间】:2016-09-27 15:23:21
【问题描述】:

这是我的代码:

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
String url="10.152.2.45";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
private class ListarEventos extends AsyncTask<String, Void, ArrayList<Evento>> {
    private OkHttpClient client = new OkHttpClient();
    @Override
    protected void onPostExecute(ArrayList<Evento> direcciones) {
        super.onPostExecute(direcciones);
    }
    @Override
    protected ArrayList<Evento> doInBackground(String... params) {
    Request request = new Request.Builder()
            .url(url)
            .build();
    try {
        Response response = client.newCall(request).execute();
        return parseResult(response.body().string());     

    } catch (IOException | JSONException e) {
        Log.d("Error",e.getMessage());
        return new ArrayList<Evento>();
    }
}
ArrayList<Evento> parseResult(String JSONstr) throws JSONException {
    ArrayList<Evento> eventos = new ArrayList<>();
    JSONObject json = new JSONObject(JSONstr);                
    JSONArray jsonEventos = json.getJSONArray("results");
    Date result=new Date ();
    for (int i=0; i<jsonEventos.length(); i++) {
        JSONObject jsonResultado = jsonEventos.getJSONObject(i);
        int jsonId = jsonResultado.getInt("Id");
        String jsonMat = jsonResultado.getString("Materia");
        String jsonTipo = jsonResultado.getString("Tipo");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH");
        String Fecha=jsonResultado.getString("Fecha");
        try {
            result = df.parse(Fecha);
        }catch (Exception e) {

        }
        String jsonDesc = jsonResultado.getString("Descripcion");
        Evento d = new Evento();
        d.Id=jsonId;
        d.Materia=jsonMat;
        d.Tipo=jsonTipo;
        d.Fecha=result;
        d.Descripcion=jsonDesc;
        eventos.add(d); 
    }
    return eventos;
}

}
}

首先,我不知道如何调用服务,如果我写了 IP 地址,它就不起作用,因为我在调试 API 时调试了 android(在同一台计算机上)。 代码应该做的是调用 API,转到 localhost/api/Eventos/Get 并获取包含所有事件的 json。

PS:我写的是电脑IP,因为如果我写localhost它不起作用

【问题讨论】:

    标签: java android asp.net json


    【解决方案1】:

    要调用在您的机器上运行的 Web API,您需要知道它的确切 url - 只是 localhost 或您的 IP 是不够的,您应该指定它正在运行的端口和它的端点,例如 localhost:8080/myapi

    另外,您应该知道 API 期望什么样的请求 - 它是否期望 GET、POST 或其他类型的请求?需要传递哪些参数?

    当您在本地启动 Web 服务时,您应该能够找到有关该服务的信息。此外,开发语言 Web 服务的调用方(在本例中为您的 Android 应用程序)没有区别 - 它可以用 ASP.NET 以外的任何其他语言编写并且行为相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 2010-11-26
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      相关资源
      最近更新 更多