我正在测试凌空应用以获取JSON数据。我的测试成功地从在线站点获取它。但是当我将它连接到我的localhost节点js服务器时它没有获取它并且没有在控制台中触发异常,根本没有响应。即使我测试到通过浏览器发送get请求并且它工作并返回JSON数据,是的,我已经推出了我的机器的实际地址而不是" localhost"。 那是我的Nodejs服务器代码:
var express=require('express');
var mongoose=require('mongoose');
//making the connections
mongoose.connect('mongodb://127.0.0.1:27017/db',function (error) {
if(error){console.log("error");}
else{
console.log("connected!!");
}
});
//using the exported function to create the server
var app=express();
// Port to be listened tos
var port=process.env.PORT || 3000;
app.get('/api',function(req,res){
res.json({firstname:'George',lastname:'Cloney'});
});
app.listen(port);
AppController.java:
public class AppController extends Application{
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
TextView text;
private String url = "http://192.168.43.214:3000/api";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.text);
JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//trying to test the response length but no results also
text.setText(" "+response.length());
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
String fullname=jsonObject.getString("firstname");
text.setText(jsonObject.toString());
Toast.makeText(getApplicationContext(),fullname,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
//Log.v("Data from the web: " , response.toString());
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Mainactivity", error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(request);
}
}
答案 0 :(得分:0)
经过长时间的分析后,我发现该对象称为响应json数组,因此解决方案是通过响应json数组而不是json对象来返回节点js代码中的json数组:
res.json([{,}];
而不是
res.json({,});
将对象响应json数组及其功能放在方法中并在onCreate上调用它:
void getJsonArray(url){
//like before
}