Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要使用复杂的JSON调用路由器_Java_Android_Json_Android Studio_Jackson - Fatal编程技术网

Java 需要使用复杂的JSON调用路由器

Java 需要使用复杂的JSON调用路由器,java,android,json,android-studio,jackson,Java,Android,Json,Android Studio,Jackson,我正在编写我的第一个Android应用程序,我刚刚开始使用JSON。我需要使用JSON将用户名和密码传输到路由器,并从路由器获得响应。但是,我不知道如何对路由器进行JSON调用。我的JSON字符串由一个JSON对象、一个数组和一个对象组成,我不知道如何向路由器发出JSON调用来传输这个复杂的JSON。我正在使用Jackson.2.2库。所以,我的问题是,如何对路由器进行JSON调用来传输这个复杂的JSON字符串 下面是我的JSON字符串: 我需要转移用户名和密码 在下面添加我的一些代码: RES

我正在编写我的第一个Android应用程序,我刚刚开始使用JSON。我需要使用JSON将用户名和密码传输到路由器,并从路由器获得响应。但是,我不知道如何对路由器进行JSON调用。我的JSON字符串由一个JSON对象、一个数组和一个对象组成,我不知道如何向路由器发出JSON调用来传输这个复杂的JSON。我正在使用Jackson.2.2库。所以,我的问题是,如何对路由器进行JSON调用来传输这个复杂的JSON字符串

下面是我的JSON字符串:

我需要转移用户名和密码

在下面添加我的一些代码:

RESTRequest.java

公共类重新请求{
私有静态最终字符串标记=JacksonUtil.class.getSimpleName();
静态字符串响应=null;
公共最终静态int GET=1;
公共最终静态int POST=2;
//无参数构造函数
公共厕所{
}
/**
*进行web服务调用
*/
公共字符串makeWebServiceCall(字符串url,int requestmethod){
返回此.makeWebServiceCall(url,requestmethod,null);
}
/**
*拨打服务电话
*/
公共字符串makeWebServiceCall(字符串URL地址、int-requestmethod、HashMap参数){
网址;
字符串response=“”;
试一试{
url=新url(url地址);
HttpURLConnection urlconn=(HttpURLConnection)url.openConnection();
urlconn.setReadTimeout(15000);
urlconn.setConnectTimeout(15000);
urlconn.setDoInput(true);
urlconn.setDoOutput(真);
if(requestmethod==POST){
urlconn.setRequestMethod(“POST”);
}else if(requestmethod==GET){
setRequestMethod(“GET”);
}
如果(参数!=null){
OutputStream OutputStream=urlconn.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(outputstream,“UTF-8”);
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
对于(Map.Entry:params.entrySet()){
如果(第一)
第一个=假;
其他的
结果。追加(&);
append(URLEncoder.encode(entry.getKey(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(entry.getValue(),“UTF-8”);
}
bufferedWriter.write(result.toString());
bufferedWriter.flush();
bufferedWriter.close();
outputstream.close();
}
int responceCode=urlconn.getResponseCode();
if(responceCode==HttpsURLConnection.HTTP\u确定){
弦线;
BufferedReader br=新的BufferedReader(新的InputStreamReader(urlconn.getInputStream());
而((line=br.readLine())!=null){
responce+=行;
}
}否则{
response=“”;
}
}捕获(例外e){
e(标记“IOException解析为json”,e);
}
返回响应;
}
千斤顶

公共类JacksonUtil{
私有静态最终字符串标记=JacksonUtil.class.getSimpleName();
私有静态对象映射器映射器;
私营静电打印机厂;
私有JacksonUtil(){
}
私有静态对象映射器getMapper(){
if(映射器==null){
映射器=新的ObjectMapper();
configure(SerializationFeature.WRITE_DATES_作为时间戳,false);
factory=mapper.getTypeFactory();
registerModule(新的JodaModule());
setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
}
返回映射器;
}
来自对象的公共静态字符串(T对象){
试一试{
返回getMapper().writeValueAsString(对象);
}捕获(IOE异常){
e(标记“IOException解析为json”,e);
}/*捕获(例外e){
Log.e(App.class.getSimpleName(),“Exception”,e);
Log.e(“JacksonUtil”、“FromObject”);
}*/
返回null;
}
公共静态T对象(InputStream为o类){
试一试{
返回getMapper().readValue(is,o);
}捕获(IOE异常){
e(标记,“从json解析IOException”,e);
}捕获(例外e){
e(标记,“从json解析异常”,e);
}
返回null;
}
公共静态T对象(字符串json,类o){
if(json==null){
返回null;
}
试一试{
返回getMapper().readValue(json,o);
}捕获(IOE异常){
e(标记,“从json解析IOException”,e);
}捕获(例外e){
e(标记,“从json解析异常”,e);
}
返回null;
}
公共静态T对象(字符串json,类型引用类型){
试一试{
返回getMapper().readValue(json,类型);
}捕获(IOE异常){
e(标记,“从json解析IOException”,e);
}捕获(例外e){
e(标记,“从json解析异常”,e);
}
返回null;
}

公共静态数据收集(String json,ClassYou已经发布了很多代码,但对我来说,你的问题究竟出在哪里并不明显。你能更具体一点吗?我需要使用json将用户名和密码传输到路由器,并从路由器获得响应,但我不知道如何向路由器发出json调用。json调用缺少代码,我不知道“我不知道怎么写,因为我的JSON字符串又是由JSON对象、数组和对象组成的。第一次使用JSON时,找不到任何有用的示例。@Jonas Seputis请编辑问题描述,并从您的评论中添加上述详细信息。”。
{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "uname", "password": "upassword"  } ] } http://192.168.1.1/ubus
public class RESTRequest {

private static final String TAG = JacksonUtil.class.getSimpleName();
static String response = null;
public final static int GET = 1;
public final static int POST = 2;

//Constructor with no parameter
public RESTRequest(){

}

/**
 * Making web service call
 */
public String makeWebServiceCall(String url, int requestmethod){
    return this.makeWebServiceCall(url, requestmethod, null);
}

/**
 * Making service call
 */
public String makeWebServiceCall(String urladdress, int requestmethod, HashMap<String, String> params){

    URL url;
    String responce = "";
    try {
        url = new URL(urladdress);

        HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
        urlconn.setReadTimeout(15000);
        urlconn.setConnectTimeout(15000);
        urlconn.setDoInput(true);
        urlconn.setDoOutput(true);

        if(requestmethod == POST){
            urlconn.setRequestMethod("POST");
        }else if(requestmethod == GET){
            urlconn.setRequestMethod("GET");
        }

        if(params != null){
            OutputStream outputstream = urlconn.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(
                    new OutputStreamWriter(outputstream, "UTF-8"));

            StringBuilder result = new StringBuilder();
            boolean first = true;
            for(Map.Entry<String, String> entry : params.entrySet()){
                if (first)
                    first = false;
                else
                    result.append("&");

                result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                result.append("=");
                result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            }

            bufferedWriter.write(result.toString());

            bufferedWriter.flush();
            bufferedWriter.close();
            outputstream.close();
        }

        int responceCode = urlconn.getResponseCode();

        if(responceCode == HttpsURLConnection.HTTP_OK){
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
            while ((line = br.readLine()) != null){
                responce += line;
            }
        }else {
            responce = "";
        }
    } catch (Exception e){
        Log.e(TAG, "IOException parsing to json", e);
    }

    return response;
}
public class JacksonUtil {

private static final String TAG = JacksonUtil.class.getSimpleName();
private static ObjectMapper mapper;
private static TypeFactory factory;

private JacksonUtil() {
}

private static ObjectMapper getMapper() {
    if (mapper == null) {
        mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        factory = mapper.getTypeFactory();
        mapper.registerModule(new JodaModule());
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    }
    return mapper;
}

public static <T> String fromObject(T object) {
    try {
        return getMapper().writeValueAsString(object);
    } catch (IOException e) {
        Log.e(TAG, "IOException parsing to json", e);
    } /*catch (Exception e) {
            Log.e(App.class.getSimpleName(), "Exception", e);
            Log.e("JacksonUtil", "FromObject");
        }*/
    return null;
}

public static <T> T toObject(InputStream is, Class<T> o) {
    try {
        return getMapper().readValue(is, o);
    } catch (IOException e) {
        Log.e(TAG, "IOException parsing from json", e);
    } catch (Exception e) {
        Log.e(TAG, "Exception parsing from json", e);
    }
    return null;
}

public static <T> T toObject(String json, Class<T> o) {
    if (json == null) {
        return null;
    }
    try {
        return getMapper().readValue(json, o);
    } catch (IOException e) {
        Log.e(TAG, "IOException parsing from json", e);
    } catch (Exception e) {
        Log.e(TAG, "Exception parsing from json", e);
    }
    return null;
}

public static <T> T toObject(String json, TypeReference<T> type) {
    try {
        return getMapper().readValue(json, type);
    } catch (IOException e) {
        Log.e(TAG, "IOException parsing from json", e);
    } catch (Exception e) {
        Log.e(TAG, "Exception parsing from json", e);
    }
    return null;
}

public static <T> T toCollection(String json, Class<? extends Collection> list, Class types) {
    try {
        return getMapper().readValue(json, factory.constructCollectionType(list, types));
    } catch (IOException e) {
        Log.e(TAG, "IOException parsing from json", e);
    } catch (Exception e) {
        Log.e(TAG, "Exception parsing from json", e);
    }
    return null;
}

public static <T> T toCollection(InputStream is, Class<? extends Collection> list, Class types) throws Exception {
    return getMapper().readValue(is, factory.constructCollectionType(list, types));
}