中文翻译大意是:自定义api响应状态错误。
拓展:对于API开发者来说,正确的错误描述甚至比仅基于Web浏览器的项目更为重要。做为使用者,咱们也能够经过浏览器消息提示清楚地了解错误以及该怎么解决。但对于API自己来讲,它们是由软件而非人员使用的,所以返回的结果应readablebymachines。这意味着HTTP状态代码就必不可少。
API给每一个请求都会返回一个状态码,请求成功一般是200,或者是以2开头的其余状态码。若是返回错误响应,则该响应不该包含2xx代码。
先拍照,再点头像,再点头像,点更换头像。
我的应用通过QQ授权成功,但不知道怎么获取用户的信息。
- (void)tencentDidLogin{。
NSLog(@"登陆完成!");。
if (_tencentOAuth.accessToken && 0 != [_tencentOAuth.accessToken length])。
// 记录登录用户的OpenID、Token以及过期时间。
NSLog(@"%@",self.tencentOAuth.accessToken);。
[self.tencentOAuth getUserInfo];//这个方法返回BOOL。
else
NSLog(@"登陆不成功 没有获取accessToken");。
- (void)getUserInfoResponse:(APIResponse*) response{。
NSLog(@"%@--",response);。
* APIResponse用于封装所有请求的返回结果,包括错误码、错误信息、原始返回数据以及返回数据的json格式字典。
*/
@interface APIResponse : NSObject<NSCoding> {。
int _detailRetCode;。
int _retCode;。
int _seq;。
NSString *_errorMsg;。
NSDictionary *_jsonResponse;。
NSString *_message;。
id _userData;。
HTTP401错误原因:用户没有访问权限,需要进行身份认证。
任何客户端 ( 例如您的浏览器) ,都需要通过以下循环:从站点的 IP 名称 ( 即您站点的网址-URL, 不带起始的 ‘http://') 获得一个 IP 地址。这个对应关系 ( 即由 IP 名称向 IP 地址转换的对应关系 ) 由域名服务器 (DNSs) 提供。
打开一个 IP 套接字 (socket) 连接到该 IP 地址。通过该套接字写 HTTP 数据流。从Web服务器接受响应的 HTTP 数据流。该数据流包括状态编码, 其值取决于 HTTP 协议 。 解析该数据流得到 状态编码和其他有用信息。
该错误在以上所述的最后一步生成,即当客户端收到 HTTP 状态编码并识别其为 401。浏览器接收到401错误信息之后,进行解释,提供一个对话框用于输入用户名/密码。在完成之后送交服务器。服务器验证之后,在验证通过的情况下把请求的资源送给浏览器,否则可能再送出401错误信息。
扩展资料:
相关错误码:
HTTP 400 - 请求无效;
HTTP 401.1 - 未授权:登录失败;
HTTP 401.2 - 未授权:服务器配置问题导致登录失败;
HTTP 401.3 - ACL 禁止访问资源;
HTTP 401.4 - 未授权:授权被筛选器拒绝;
HTTP 401.5 - 未授权:ISAPI 或 CGI 授权失败。
参考资料来源:百度百科-401错误。
java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已给你两个方法吧:
public static String do_post(String url, List<NameValuePair> name_value_pair) throws IOException { String body = "{}"; DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpPost httpost = new HttpPost(url); httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8)); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); body = EntityUtils.toString(entity); } finally { httpclient.getConnectionManager().shutdown(); } return body; } public static String do_get(String url) throws ClientProtocolException, IOException { String body = "{}"; DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity ent发处篡肺诂镀磋僧单吉ity = response.getEntity(); body = EntityUtils.toString(entity); } finally { httpclient.getConnectionManager().shutdown(); } return body; }。
以下内容属于引用复制,但可以回复你的问题:
准备工作:在libs下添加 alicloud-Android-apigateway-sdk-1.0.1.jar,commons-codec-1.10-1.jar。
在build.gradle添加 compile'com.squareup.okhttp3:okhttp:3.4.1'。
在onCreate,或者afterViews初始化API网关。
private void initGatewaySdk() {。
// 初始化API网关
ApiGatewayClient.init(getApplicationContext(), false);。
调用拍照功能
private void getPhotoCard(){。
final String status = Environment.getExternalStorageState();。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {。
requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, CAMERA_REQUEST_CODE);。
if (status.equals(Environment.MEDIA_MOUNTED)) {。
defaultPhotoAddress = PHOTO_DIR + "/" + getPhotoName();。
PreferenceUtils.modifyStringValueInPreferences(CardActivity.this, Preferences.IMAGE_3, defaultPhotoAddress);。
imageUri = Uri.fromFile(new File(defaultPhotoAddress));。
Intent intentPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture。
intentPhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);。
startActivityForResult(intentPhoto, CAMERA_REQUEST_CODE);。
} else {
Toast.makeText(CardActivity.this, "没有sd卡", Toast.LENGTH_SHORT).show();。
可以对照片进行裁剪
private void cropImageUri(Uri desUri, int outputX, int outputY, int requestCode){。
Intent intent = new Intent("com.android.camera.action.CROP");。
intent.setDataAndType(desUri, "image/*");。
intent.putExtra("crop", "true");。
intent.putExtra("aspectX", 5);。
intent.putExtra("aspectY", 3);。
intent.putExtra("outputX", outputX);。
intent.putExtra("outputY", outputY);。
intent.putExtra("scale", true);。
intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri);。
intent.putExtra("return-data", false);。
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());。
intent.putExtra("noFaceDetection", true);。
startActivityForResult(intent, requestCode);。
用base64上传照片
private void trygetCardNum() {。
if (NetworkUtils.isConnectWithTip(this, "您未连接网络,无法获取数据")) {。
LoadingUtil.show(this);。
String imgBase64 = "";。
try {
defaultPhotoAddress = PreferenceUtils.getStringValueInPreferences(this, Preferences.IMAGE_3);。
File file = new File(defaultPhotoAddress);。
byte[] content = new byte[(int) file.length()];。
FileInputStream finputstream = new FileInputStream(file);。
finputstream.read(content);。
finputstream.close();。
imgBase64 = new String(Base64.encodeBase64(content));。
// imgBase64 = Base64Img.Bitmap2StrByBase64(bitmap);。
if(imgBase64!=null||!imgBase64.equals("")){。
PreferenceUtils.modifyStringValueInPreferences(CardActivity.this, Preferences.IMAGE_3, null);。
} catch (IOException e) {。
e.printStackTrace();。
return;
// 获取服务
RpcService rpcService = ApiGatewayClient.getRpcService();。
final ApiRequest apiRequest = new ApiRequest();。
// 设置请求地址、Path及Method。
apiRequest.setAddress("https://dm-51.data.aliyun.com");。
apiRequest.setPath("/rest/160601/ocr/ocr_idcard.json");。
apiRequest.setMethod(HttpMethod.POST);。
// 按照文档设置二进制形式Body,支持设置Query参数、Header参数、Form形式Body。
apiRequest.setStringBody("{\"inputs\":[{\"image\":{\"dataType\":50,\"dataValue\":\""+imgBase64+"\"},\"configure\":{\"dataType\":50,\"dataValue\":\"{\\\"side\\\":\\\"face\\\"}\"}}]}");。
// 设置支持自签等形式的证书,如果服务端证书合法请勿设置该值,仅在开发测试或者非常规场景下设置。
apiRequest.setTrustServerCertificate(true);。
// 设置超时
apiRequest.setTimeout(10000);。
rpcService.call(apiRequest, new ApiResponseCallback() {。
@Override
public void onSuccess(ApiResponse apiResponse) {。
// 处理apiResponse。
LoadingUtil.dismiss();。
String s = apiResponse.getStringBody();。
NumBean result = JSONObject.parseObject(s, NumBean.class);。
String dataValue = result.getOutputs().get(0).getOutputValue().getDataValue();。
DataValueBean dataValueBean = JSONObject.parseObject(dataValue, DataValueBean.class);。
Number = dataValueBean.getNum();。
name = dataValueBean.getName();。
address = dataValueBean.getAddress();。
birth = dataValueBean.getBirth();。
nationality = dataValueBean.getNationality();。
sex = dataValueBean.getSex();。
if(dataValueBean.getError_msg()==null){。
runOnUiThread(new Runnable() {。
public void run() {。
if(Number == null||Number.equals("")||name == null||name.equals("")||address == null||address.equals("")||birth == null||birth.equals("")||sex == null||sex.equals("")){。
Toast.makeText(CardActivity.this, "扫描失败,请重试", Toast.LENGTH_LONG).show();。
}else {
//扫描成功
});
}else {
errString = dataValueBean.getError_msg();。
runOnUiThread(new Runnable() {。
public void run() {。
Toast.makeText(CardActivity.this, "扫描失败,请重试", Toast.LENGTH_LONG).show();。
});
@Override
public void onException(ApiInvokeException e) {。
// 处理异常
LoadingUtil.dismiss();。
runOnUiThread(new Runnable() {。
public void run() {。
Toast.makeText(CardActivity.this, "扫描失败,请重试", Toast.LENGTH_LONG).show();。
});
});
附:
public class NumBean implements Serializable{。
private List<OutputsBean> outputs;。
public List<OutputsBean> getOutputs() {。
return outputs;。
public void setOutputs(List<OutputsBean> outputs) {。
this.outputs = outputs;。
public static class OutputsBean {。
private String outputLabel;。
private OutputMultiBean outputMulti;。
private OutputValueBean outputValue;。
public String getOutputLabel() {。
return outputLabel;。
public void setOutputLabel(String outputLabel) {。
this.outputLabel = outputLabel;。
public OutputMultiBean getOutputMulti() {。
return outputMulti;。
public void setOutputMulti(OutputMultiBean outputMulti) {。
this.outputMulti = outputMulti;。
public OutputValueBean getOutputValue() {。
return outputValue;。
public void setOutputValue(OutputValueBean outputValue) {。
this.outputValue = outputValue;。
public static class OutputMultiBean {。
public static class OutputValueBean {。
private int dataType;。
private String dataValue;。
public int getDataType() {。
return dataType;。
public void setDataType(int dataType) {。
this.dataType = dataType;。
public String getDataValue() {。
return dataValue;。
public void setDataValue(String dataValue) {。
this.dataValue = dataValue;。
public class DataValueBean implements Serializable{。
private String address;。
private String birth;。
private String config_str;。
private String error_msg;。
private String name;。
private String nationality;。
private String num;。
private String request_id;。
private String sex;。
private boolean success;。
public String getAddress() {。
return address;。
public void setAddress(String address) {。
this.address = address;。
public String getBirth() {。
return birth;
public void setBirth(String birth) {。
this.birth = birth;。
public String getConfig_str() {。
return config_str;。
public void setConfig_str(String config_str) {。
this.config_str = config_str;。
public String getError_msg() {。
return error_msg;。
public void setError_msg(String error_msg) {。
this.error_msg = error_msg;。
public String getName() {。
return name;
public void setName(String name) {。
this.name = name;。
public String getNationality() {。
return nationality;。
public void setNationality(String nationality) {。
this.nationality = nationality;。
public String getNum() {。
return num;
public void setNum(String num) {。
this.num = num;。
public String getRequest_id() {。
return request_id;。
public void setRequest_id(String request_id) {。
this.request_id = request_id;。
public String getSex() {。
return sex;
public void setSex(String sex) {。
this.sex = sex;。
public boolean isSuccess() {。
return success;。
public void setSuccess(boolean success) {。
this.success = success;。
在AndroidManifest.xml下添加。
<meta-data android:name="com.alibaba.apigateway.appKey" android:value="" />。
<meta-data android:name="com.alibaba.apigateway.appSecret" android:value="" />。