Xutils and Fastjson acquisition and Beijing-racing platform rental analytic network data

Source: Internet
Author: User

Send a Beijing-racing platform rental asynchronous tasks are JSON data

First open an interface, which is JSON data, through the Xutils framework to send a GET request, get the interface JSON string, the string is parsed into an entity class Fastjson, stored in the collection
Add Library project to Gradle

xUtils框架:compile ‘org.xutils:xutils:3.3.40’FastJson:compile ‘com.alibaba:fastjson:1.1.56.android’(我也不知道为什么显示出来的android是红色大写,应当是android)

Implementation of the entity class (VO Class)

首先我们要有一个可以打开的接口:http://103.244.59.105:8014/paopaoserver/articles?params={“page”:1,”page_count”:10}内容:这里写图片描述分析他的结构创建一个日常用品的Vo类,一般名字和id都是比较重要的

Private String Classify_name;
private int classify_id;

public int getClassify_id() {    return classify_id;}public void setClassify_id(int classify_id) {    this.classify_id = classify_id;}public String getClassify_name() {    return classify_name;}public void setClassify_name(String classify_name) {    this.classify_name = classify_name;}123456789101112131415161718123456789101112131415161718

The properties of the Vo class are the same as the property names in the JSON data so that they can be parsed correctly
With a VO entity class, we can map the JSON
Create a parser

Create a parser that can be used to parse data from a parser throughout the project

Creating a parser is implementing the Responseparser interface
public class Jsonrespondparse implements Responseparser {br/> @Override
< p="">

}

Overriding methods for parsing JSON data br/> @Override

Determine whether the entire data is wrapped up by a list
if (ResultClass = = List.class) {
Parses a list into small objects (list's name is Datas)
result = Json.parseobject (result). getString ("Datas");
Parse into a small Vo object
return Json.parsearray (result, (class<?>) Parameterizedtypeutil.getparameterizedtype (Resulttype, List.class, 0));
} else {//If it is not made of list, the JSON is parsed directly into the VO entity class
return Json.parseobject (result, resultclass);
}

}

}

123456789101112131415161718192021123456789101112131415161718192021

Consider the parser as an annotation of the Vo class

Annotations added at the top of the Vo class
@HttpResponse (parser = jsonrespondparse.class)
This allows the JSON data to be mapped directly to the Vo object and put the object into the list collection
Vo Class All code:

@HttpResponse (parser = jsonrespondparse.class)
public class Daygoodsvo {
Private String Classify_name;
private int classify_id;

public int getClassify_id() {    return classify_id;}public void setClassify_id(int classify_id) {    this.classify_id = classify_id;}public String getClassify_name() {    return classify_name;}public void setClassify_name(String classify_name) {    this.classify_name = classify_name;}@Overridepublic String toString() {    return classify_name;}

}

12345678910111213141516171819202122232425261234567891011121314151617181920212223242526

Get JSON data

The next step is to get the JSON data, and only the string that gets the JSON data can parse it.

Here to use the Xutils framework to send a GET request to get the data, the use of the Xutils framework is not introduced here
First, we need to splice the interface.

Interface URL
Requestparams requestparams = new Requestparams
("Http://103.244.59.105:8014/paopaoserver/articles");
Splicing interface (parameter name, parameter)
Requestparams.addquerystringparameter ("params", "{\" page\ ": 1,\" page_count\ ": 10}");

1234512345

So the complete URL is stitched up.
Send a GET request, get the data, and parse the data

Use X.http (). Get to send a GET request (URL, get back data and manipulate)
X.http (). Get (Requestparams, new callback.cachecallback<list<daygoodsvo>> () {br/>//get returned data will override five methods
@Override

You can get the list collection directly when you return to success
If it was string.class, it would be string strings.
for (Daygoodsvo Daygoods:result) {
LOG.E ("tag", daygoods.tostring ());
Mlist.add (Daygoods);
}
Put the parsed data into the adapter and display it in the ListView
Mlistview.setadapter (New Daybaseadapter (Daygoodsactivity.this, mlist));
}

        @Override        public void onError(Throwable ex, boolean isOnCallback) {        }        @Override        public void onCancelled(CancelledException cex) {        }        @Override        public void onFinished() {        }        @Override        public boolean onCache(List<DayGoodsVo> result) {            return false;        }    });}12345678910111213141516171819202122232425262728293031323334353637381234567891011121314151617181920212223242526272829303132333435363738

X.http () has a sub-thread in it, so there is no need to open a single child thread to implement the network request, and the data is displayed in the ListView directly in the Onsuccess ().
The following is the complete code

private void Gethttpdata () {
Requestparams requestparams = new Requestparams
("http://103.244.59.105:8014/ Paopaoserver/articles ");
Requestparams.addquerystringparameter ("params", "{\" page\ ": 1,\" page_count\ ": 10}");
X.http (). Get (Requestparams, New callback.cachecallback<list<daygoodsvo>> () {br/> @Override
for (Daygoodsvo daygoods:result) {
log.e ("tag", Daygoods.tostring ());
Mlist.add (daygoods);
}
Mlistview.setadapter (New Daybaseadapter (Daygoodsactivity.this, mlist));
}

        @Override        public void onError(Throwable ex, boolean isOnCallback) {        }        @Override        public void onCancelled(CancelledException cex) {        }        @Override        public void onFinished() {        }        @Override        public boolean onCache(List<DayGoodsVo> result) {            return false;        }    });}1234567891011121314151617181920212223242526272829303132333435363712345678910111213141516171819202122232425262728293031323334353637

This enables the JSON data to be parsed directly into a VO entity class through an asynchronous task
Send a sync task

To access the network in a synchronous thread, it must be in a child thread
public void Httpgetsync () {
New Thread (New Runnable () {br/> @Override

Requestparams requestparams = new Requestparams (Urljson);
Add a request to get information
The form of a key-value pair that encapsulates the request parameter
Requestparams.addquerystringparameter ("Sex", "girl");
try {
String result = X.http (). Getsync (Requestparams, String.class);

                Log.d("TAG", "result:" + result);            } catch (Throwable throwable) {                throwable.printStackTrace();            }        }    }).start();}123456789101112131415161718192021123456789101112131415161718192021

This method gets a string literal, or it can be parsed directly into the list
Uploading files

Uploading files
private void Upload () {
URL for uploading files
Requestparams requestparams = new Requestparams (FILEURL);
Emulate a form, simulate uploading a file
The upload is a form, and you can upload the file
Requestparams.setmultipart (TRUE);
Upload a file (define a name for the file file,head, upload the file or avatar)
Requestparams.addbodyparameter ("File", new file (FilePath));
Send upload request, GET request cannot upload form request, want to use POST request
X.http (). Post (Requestparams, New callback.commoncallback<string> () {br/> @Override

LOG.D ("TAG", "uoload success");

        }        @Override        public void onError(Throwable ex, boolean isOnCallback) {        }        @Override        public void onCancelled(CancelledException cex) {        }        @Override        public void onFinished() {        }    });}

Xutils and Fastjson acquisition and Beijing-racing platform rental analytic network data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.