求高人帮助看一下这个类型的报错如何修改?

CJCO008159 北京邮电大学 学生  2017-01-09 05:11:44
高手,您好:
小弟的代码如下:
package n_port_package_MyGodWillMyIdeal;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import net.sf.json.JSONObject;

public class AddressUtils {
static String country = "";
static String area = "";
static String region = "";
static String city = "";
static String enterprise = "";
static String ISP = "";

/**
* @param args
*/
public static void main(String[] args) {

AddressUtils addressUtils = new AddressUtils();

String ip = "222.75.255.81";

String address = "";

try {

address = addressUtils.getAddress("ip=" + ip, "utf-8");

} catch (Exception e) {
e.printStackTrace();

}

// //////////System.out.println(address);
// //////////System.out.println("region-------->>"+region);
// //////////System.out.println("city---------->>"+city);
// //////////System.out.println("country---------->>"+country);
// //////////System.out.println("ISP---------->>"+ISP);

}

/**
* 获取地址
*
* @param params
* @param encoding
* @return
* @throws Exception
*/
public String getAddress(String params, String encoding) throws Exception {

String path = "http://ip.taobao.com/service/getIpInfo.php";

String returnStr = this.getRs(path, params, encoding);

JSONObject json = null;

if (returnStr != null) {

json = new JSONObject(returnStr);

if ("0".equals(json.get("code").toString())) {

StringBuffer buffer = new StringBuffer();

// buffer.append(decodeUnicode(json.optJSONObject("data").getString("country")));//国家

// buffer.append(decodeUnicode(json.optJSONObject("data").getString("area")));//地区

buffer.append(decodeUnicode(json.optJSONObject("data")
.getString("region")));// 省份
region = decodeUnicode(json.optJSONObject("data").getString(
"region"));
buffer.append(decodeUnicode(json.optJSONObject("data")
.getString("city")));// 市区
city = decodeUnicode(json.optJSONObject("data").getString(
"city"));
buffer.append(decodeUnicode(json.optJSONObject("data")
.getString("county")));// 地区
country = decodeUnicode(json.optJSONObject("data").getString(
"county"));
buffer.append(decodeUnicode(json.optJSONObject("data")
.getString("isp")));// ISP公司
ISP = decodeUnicode(json.optJSONObject("data").getString("isp"));
// //////////System.out.println(buffer.toString());
return buffer.toString();

} else {

return "用户地址获取暂不成功";

}

}

return null;

}

/**
* 从url获取结果
*
* @param path
* @param params
* @param encoding
* @return
*/
public String getRs(String path, String params, String encoding) {

URL url = null;

HttpURLConnection connection = null;

try {

url = new URL(path);

connection = (HttpURLConnection) url.openConnection();// 新建连接实例

connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫

connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫

connection.setDoInput(true);// 是否打开输出 true|false

connection.setDoOutput(true);// 是否打开输入流true|false

connection.setRequestMethod("POST");// 提交方法POST|GET

connection.setUseCaches(false);// 是否缓存true|false

connection.connect();// 打开连接端口

DataOutputStream out = new DataOutputStream(connection
.getOutputStream());

out.writeBytes(params);

out.flush();

out.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), encoding));

StringBuffer buffer = new StringBuffer();

String line = "";

while ((line = reader.readLine()) != null) {

buffer.append(line);

}

reader.close();

return buffer.toString();

} catch (Exception e) {

e.printStackTrace();

} finally {

connection.disconnect();// 关闭连接

}

return null;
}

/**
* 字符转码
*
* @param theString
* @return
*/
public static String decodeUnicode(String theString) {

char aChar;

int len = theString.length();

StringBuffer buffer = new StringBuffer(len);

for (int i = 0; i < len;) {

aChar = theString.charAt(i++);

if (aChar == '\\') {

aChar = theString.charAt(i++);

if (aChar == 'u') {

int val = 0;

for (int j = 0; j < 4; j++) {

aChar = theString.charAt(i++);

switch (aChar) {

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

val = (val << 4) + aChar - '0';

break;

case 'a':

case 'b':

case 'c':

case 'd':

case 'e':

case 'f':

val = (val << 4) + 10 + aChar - 'a';

break;

case 'A':

case 'B':

case 'C':

case 'D':

case 'E':

case 'F':

val = (val << 4) + 10 + aChar - 'A';

break;

default:

throw new IllegalArgumentException(

"Malformed encoding.");
}

}

buffer.append((char) val);

} else {

if (aChar == 't') {

aChar = '\t';
}

if (aChar == 'r') {

aChar = '\r';
}

if (aChar == 'n') {

aChar = '\n';
}

if (aChar == 'f') {

aChar = '\f';

}

buffer.append(aChar);
}

} else {

buffer.append(aChar);

}

}

return buffer.toString();

}

}

高手,小弟的上面的这个类型的报错在这个位置:

求高人点拨:
小弟的上述类型,这个小弟从网上Dwon下来的工具类,要怎么修改,才能够将图片中的报错改掉?
谢谢高人的过目!!
...全文
78 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
逗你玩-- 2017-01-09
JSONObject json = JSONObject.fromObject(str); 就行了
回复
海澜百丈冰 2017-01-09
要么你这个项目本身问题,要么就是你引错了类
回复
soton_dolphin 2017-01-09
你要花点时间先看看API
回复
A_lihaichao 2017-01-09
楼主这属于是没有定义,你要调用方法得先看一下参数类型是不是对的
回复
soton_dolphin 2017-01-09
SONObject json = JSONObject.fromObject(str);
回复
soton_dolphin 2017-01-09
没有String作为参数的JSONObject构造器
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2017-01-09 05:11
社区公告
暂无公告