我使用Discourse API从python访问urlfetch。 按用户名获取单个用户端点需要GET请求,例如/users/{username}.json
在浏览器中,此命令按预期返回json响应,但是来自API调用,如:
from google.appengine.api import urlfetch
result = urlfetch.fetch('{}/users/{}.json'.format(domain, username))
它返回一个HTML页面。我甚至尝试将内容类型设置为application / json:
headers = {'Content-Type': 'application/json'}
result = urlfetch.fetch('{}/users/{}.json'.format(domain, username), headers=headers)
我做错了什么?
答案 0 :(得分:0)
<强>解决:强>
需要将api_key和api_username添加到GET请求:
@Override
public void onResponse(String response) {
try {
JSONObject mainJsonObject =new JSONOnject(response); // this is main reason for exception.
//change below logic as per your json getting as string response
JSONArray jsonArray=mainJsonObject.getJSONArray("name");
JSONObject jsonObject=jsonArray.getJSONObject(0);
String code=jsonObject.getString("code");
if (code.equals("login_failed")){
builder.setTitle("Login Error");
displayAlert(jsonObject.getString("message"));
}
else{
Intent in= new Intent(login.this,welcome.class);
Bundle bundle= new Bundle();
bundle.putString("name",jsonObject.getString("name"));
in.putExtras(bundle);
startActivity(in);
}
} catch (JSONException e) {
e.printStackTrace();
}
}