不知道错在哪里,不知道哪位高人能赐教?
package chazhao;
import java.io.FileWriter;
import java.io.IOException;
public class chazhao {
/**
* 将手机号码转换为短信验证码请求参数字符串
* @param phoneNum 手机号码
* @return 短信验证码请求参数字符串
*/
public static String getRequestParams(String phoneNum) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("手机号码.txt");
String httpArg = ""; // 初始化 HTTP 请求参数字符串
for (int i = 0; i < 9999; i++) {
if (i >= 1000) {
httpArg = "phone=135" + String.valueOf(i) + "0015";//String.valueOf(i)是将整数i转换为字符串类型的方法。具体来说,它会将i的值转换为对应的字符序列,并返回这个字符序列所表示的字符串
} else if (i >= 100) {
httpArg = "phone=1350" + String.valueOf(i) + "0015";
} else if (i >= 10) {
httpArg = "phone=13500" + String.valueOf(i) + "0015";
} else {
httpArg = "phone=135000" + String.valueOf(i) + "0015";
}
if (Czhao.city.contains("上海")) {
fileWriter.write(Czhao.city + "\t");// 将符合条件的请求参数写入文件中,使用制表符分隔不同的参数
}
fileWriter.flush();// 将缓冲区中的数据刷新到磁盘上
}
return httpArg;
} catch (IOException e) {
e.printStackTrace();// 如果出现 IO异常,则打印异常信息
System.out.println("文件写入失败,请检查文件路径和权限!");
if (fileWriter != null) {
try {
fileWriter.close();// 关闭文件流对象
} catch (IOException ex) {
}
}
return null;// 返回null表示异常处理失败
} finally {
if (fileWriter != null) {
try {
fileWriter.close();// 关闭文件流对象
} catch (IOException ex) {
}
}
}
}
}
package chazhao;
import org.json.*;
import com.baidubce.http.*;
import com.baidubce.model.*;
// 号码归属查询api Java示例代码
public class Czhao {
public static String city = "";
public static void main(String[] args) {
String path = "http://gsd.api.bdymkt.com/sms";
ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
request.setCredentials("*****************", "****************");
request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
request.addQueryParameter("mobile", "13598768415");
String requestExample = "\r\nimport com.baidubce.http.ApiExplorerClient;\r\nimport com.baidubce.http.AppSigner;\r\nimport com.baidubce.http.HttpMethodName;\r\nimport com.baidubce.model.ApiExplorerRequest;\r\nimport com.baidubce.model.ApiExplorerResponse;\r\n\r\n// 号码归属查询api Java示例代码\r\npublic class RequestDemo {\r\n public static void main(String[] args) {\r\n String path = \"http:\\\\gsd.api.bdymkt.comhttp:\\\\gwgp-g8eennmvmcz.n.bdcloudapi.com\\sms\";\r\n ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);\r\n request.setCredentials(\" 您的 access key\", \"您的 secret key\");\r\n\r\n request.addHeaderParameter(\"Content-Type\", \"application\\json;charset=UTF-8\");\r\n \r\n request.addQueryParameter(\"mobile\", \"\");\r\n \r\n \r\n\r\n ApiExplorerClient client = new ApiExplorerClient(new AppSigner());\r\n\r\n try {\r\n ApiExplorerResponse response = client.sendRequest(request);\r\n \\\\ 返回结果格式为Json字符串\r\n System.out.println(response.getResult());\r\n } catch (Exception e) {\r\n e.printStackTrace();\r\n }\r\n }\r\n}";
request.setJsonBody(requestExample);
ApiExplorerClient client = new ApiExplorerClient(new AppSigner());
try {
ApiExplorerResponse response = client.sendRequest(request);// 返回结果格式为Json字符串
String result = new String(response.getResult());// 从response对象中获取名为"result"的子属性的值,并将Json字符串转化为String型
JSONObject jsonObject = new JSONObject(result);//一个字符串类型的 JSON 数据转换为 JSONObject 对象。具体来说,它会将字符串中的 JSON 数据解析出来,并将其存储在一个 JSONObject 对象中
city = jsonObject.getString("City");
//System.out.println(response.getResult());
} catch (Exception e) {
e.printStackTrace();
}
}
}
打印以下 result 字符串,看下具体的数据是啥?
你这里直接转换成 json对象,然后取City 报错,说明返回的result 有问题
json解析正常,可以在city那一行设置断点,看看jsonObject的结构,city在哪个节点下
先打印下result,看下JSON的格式,现在的取数取不到数据