我正在尝试将Android json输出与Ruby Web应用程序连接。 我很难将Android json post请求与Ruby应用程序的接收连接起来。当Android jsonobject里面有另一个jsonobject时,它在ruby应用程序中无法识别。这是我的代码。
Android代码
JSONObject events_array = new JSONObject();
JSONObject parent=new JSONObject();
for (int i = 0; i < classtimeList.size(); i++) {
events_array.put(classtimeList.get(i).toString(),priorityList.get(i).toString());
}
parent.put("token","token_information");
parent.put("class_type", "something");
parent.put("class_frequency", "5");
parent.put("course_id", "20");
parent.put("events_array", events_array);
String urlParameters = "info="+parent.toString();
Log.i("parameters", urlParameters);
这是参数的日志信息。
info={"token":"token_information","class_type":"something","class_frequency":"5","course_id":"20","events_array":{"3074":"3","3134":"1","3140":"1","3146":"3","3152":"1","3075":"3","3076":"3","3077":"3","3078":"3","3079":"3","3216":"3","3217":"3","3218":"1","3219":"3"}}
我将此信息传递给Ruby应用程序,我很难提取“events_array”信息。下面是我的Ruby代码。
我的完整Android代码看起来像
class apply extends AsyncTask<String, String, String> {
ProgressDialog pd;
private Exception exception;
protected void onPreExecute() {
pd = new ProgressDialog(ClassApply2.this);
pd.setMessage("loading");
pd.show();
}
protected String doInBackground(String... args) {
String token = args[0];
try {
URL url = new URL("https://www.ringleplus.com/api/v1/apply/test");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
JSONObject events_array = new JSONObject();
JSONObject parent=new JSONObject();
for (int i = 0; i < classtimeList.size(); i++) {
events_array.put(classtimeList.get(i).toString(),priorityList.get(i).toString());
}
parent.put("token","token_information");
parent.put("class_type", "something");
parent.put("class_frequency", "5");
parent.put("course_id", "20");
parent.put("events_array", events_array);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
String urlParameters = "info=" + parent.toString();
DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
dStream.writeBytes(urlParameters);
//dStream.write(data); // <!-- 여기에 url parameter가 들어감
dStream.flush();
dStream.close();
Log.i("parameters", parent.toString());
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line).append("\n");
}
reader.close();
connection.disconnect();
JSONObject jsonObj = null;
jsonObj = new JSONObject(buffer.toString().trim());
String output = jsonObj.getString("item");
return output;
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
//progressBar.setVisibility(View.GONE);
Log.i("INFO", response);
if (pd != null) {
pd.dismiss();
}
adapter.notifyDataSetChanged();
}
Ruby Code
post:test do
parsed_json = ActiveSupport::JSON.decode(params[:info].to_json)
token = parsed_json["token"]
events_array = parsed_json["events_array"]
output = Array.new
if events_array != nil
events_array.each do |start_time, priority|
if priority.to_i == 1 || priority.to_i == 2
userapply = Userapply.new
userapply.classtime_id = start_time
userapply.user_id = user.id
userapply.priority = priority
userapply.save
output << {:userapply_id => userapply.id, :classtime_id => userapply.classtime_id, :user_id => userapply.user_id, :priority => userapply.priority}
end
end
end
return {status: 'ok', item: events_array}
end
它应该返回的是有关events_array的信息(即{“3074”:“3”,“3134”:“1”,“3140”:“1”,“3146”:“3”,“ 3152 “:” 1" , “3075”: “3”, “3076”: “3”, “3077”: “3”, “3078”: “3”, “3079”: “3”, “3216” :“3”,“3217”:“3”,“3218”:“1”,“3219”:“3”})
但是,Android输出日志是 I / INFO:events_array
但令牌提取似乎有效。
token = parsed_json["token"]
如果这样可行,那么我的直觉就是parsed_json [“events_array”]也应该在某种意义上起作用。但我并没有被困在这里。
如果这个问题得到解决,那么我希望收到parsed_json [“events_array”]作为哈希信息,并希望处理完整代码并获取“输出”而不是events_array来检查为什么这不起作用。
如果有什么我遗失的,请告诉我。我真的需要一些帮助。
期待看到任何人的回复。
答案 0 :(得分:3)
我终于弄清楚了问题的来源。我将您发送到服务器的内容与上面评论中的curl以及您尝试从android发送的内容进行了比较。而这两件事情完全不同。要从androdid发送相同内容,您应该像这样形成您的urlParameter:
JSONObject info = new JSONObject();
info.put("info",parent);
urlParameters = info.toString();
并且忘记在connection.setRequestProperty("Content-Type", "application/json")
之后忘记connection.setDoOutput(true)
。
我在android之外进行了测试,它的回复与上面评论中的curl命令相同。它应该在android中运行得很好。
PS:请下次更加细心。