简体   繁体   English

改造 Android 应用程序从 Node.js 服务器获取 JSON

[英]Retrofit Android Application GET JSON from Node.js Server

I solved the problem.我解决了这个问题。 I was sending plain-text in the server.我在服务器中发送纯文本。 Changed the line to read:将行更改为:

response.writeHead(200, {'Content-Type': 'application/json'});

Quick question.快速提问。 My friend recommended looking into Retrofit rather than using my ASync task for my REST android application.我的朋友建议研究 Retrofit 而不是将我的 ASync 任务用于我的 REST android 应用程序。 I am having one small problem since I am new to the system.我有一个小问题,因为我是系统的新手。 The server runs using Node.js to send JSON objects by means of .stringify() and so when I retrieve an object it is a String rather than a JSON, and I cannot convert it to my List of the appropriate objects.服务器使用 Node.js 运行以通过 .stringify() 发送 JSON 对象,因此当我检索一个对象时,它是一个字符串而不是 JSON,我无法将其转换为我的适当对象列表。 I am guessing the error is due to a cast exception because it is sending a string because I get the exception:我猜这个错误是由于强制转换异常,因为它正在发送一个字符串,因为我得到了异常:

com.google.gson.JsonSyntaxException

Any help would be great.任何帮助都会很棒。 Here is the code I thought would be appropriate.这是我认为合适的代码。

Android Client Code Example below: Android客户端代码示例如下:

private void requestData() {
    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ENDPOINT).build();
    UnitAPI unit_api = adapter.create(UnitAPI.class);

    unit_api.getUnits(new Callback<List<Unit>>() {

        @Override
        public void success(List<Unit> t, Response response) {
            units = t;
            updateDisplay();
        }

        @Override
        public void failure(RetrofitError error) {
            Log.d("Failure UnitGet()", error.getMessage());

        }
    });

}

Android Unit.class: Android Unit.class:

public class Unit {

private String ID;
private long bearing;
private long lat;
private long lng;
private String type;

public String getID() {
    return ID;
}
public void setID(String ID){
    this.ID = ID;
}
public long getBearing() {
    return bearing;
}
public void setBearing(long bearing){
    this.bearing = bearing;
}
public void setLat(long lat){
    this.lat = lat;
}
public long getLat(){
    return lat;
}
public void setLng(long lng){
    this.lng = lng;
}
public long getLng(){
    return lng;
}
public void setType(String type){
    this.type = type;
}
public String getType(){
    return type;
}
}

The server sends the .json file via the command:服务器通过以下命令发送 .json 文件:

if (method == "GET") {                                            
  response.write(JSON.stringify(unitsJSON));
}

Any thoughts on how I can convert it to a list of units from the string on the client side or a json object on the server side?关于如何将其从客户端的字符串或服务器端的 json 对象转换为单位列表的任何想法?

Thanks!谢谢!

I solved the problem.我解决了这个问题。 I was sending plain-text in the server.我在服务器中发送纯文本。 Changed the line to read:将行更改为:

response.writeHead(200, {'Content-Type': 'application/json'});

I think you must add json-parser to package using npm then我认为您必须使用 npm 将json-parser添加到包中
in response write作为回应写

res.end(JSON.stringfy('your resp0onse here'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM