java - Android App - Unable to start activity ComponentInfo -
i created app, should receive data thingspeak channel. first used webview widget, want go further , use thingspeak java api handle data myself.
in mainactivity put in code :
channel channel = new channel(1234,"writekey"); try { entry entry = channel.getlastchannelentry(); } catch (unirestexception e) { e.printstacktrace(); } catch (thingspeakexception e) { e.printstacktrace(); } out.println("entry");
but following error:
java.lang.runtimeexception: unable start activity componentinfo{de.babytemp.babytempapp2/de.babytemp.babytempapp2.mainactivity}: java.lang.illegalargumentexception: unknown pattern character 'x' @ android.app.activitythread.performlaunchactivity(activitythread.java:2325) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2387) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1303) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.illegalargumentexception: unknown pattern character 'x' @ java.text.simpledateformat.validatepatterncharacter(simpledateformat.java:314) @ java.text.simpledateformat.validatepattern(simpledateformat.java:303) @ java.text.simpledateformat.<init>(simpledateformat.java:356) @ com.google.gson.defaultdatetypeadapter.<init>(defaultdatetypeadapter.java:49) @ com.google.gson.gsonbuilder.addtypeadaptersfordate(gsonbuilder.java:555) @ com.google.gson.gsonbuilder.create(gsonbuilder.java:543) @ de.babytemp.babytempapp2.channel.<init>(channel.java:46) @ de.babytemp.babytempapp2.mainactivity.oncreate(mainactivity.java:41) @ android.app.activity.performcreate(activity.java:5990) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) @ android.app.activitythread.performlaunchactivity(activitythread.java:2278) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2387) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1303) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
thanks!
package de.babytemp.babytempapp2; import com.google.gson.gson; import com.google.gson.gsonbuilder; import com.mashape.unirest.http.httpresponse; import com.mashape.unirest.http.jsonnode; import com.mashape.unirest.http.unirest; import com.mashape.unirest.http.exceptions.unirestexception; import com.mashape.unirest.request.getrequest; import java.util.hashmap; public class channel { private string apiurl = "http://api.thingspeak.com"; private static final string apiheader = "xxxxxxxxx"; private final integer channelid; private string readapikey; private string writeapikey; private final boolean ispublic; private final hashmap<string, object> fields = new hashmap<>(); private final gson gson = new gsonbuilder().setdateformat("yyyy-mm-dd't'hh:mm:ss").create(); /** * constructor public, read-only, thingspeak channel. type of * channel cannot updated. * * @param channelid channel id. */ public channel(integer channelid) { this.ispublic = true; this.channelid = channelid; } /** * constructor public, writeable, thingspeak channel. * * @param channelid channel id. * @param writekey api key channel. see * https://thingspeak.com/channels/<channelid>#apikeys */ public channel(integer channelid, string writekey) { this.ispublic = true; this.channelid = channelid; this.writeapikey = writekey; } /** * constructor private, writeable, thingspeak channel. * * @param channelid channel id. * @param writekey write api key. see * https://thingspeak.com/channels/<channelid>#apikeys. * @param readkey read api key. see * https://thingspeak.com/channels/<channelid>#apikeys. */ public channel(integer channelid, string writekey, string readkey) { this.channelid = channelid; this.readapikey = readkey; this.writeapikey = writekey; this.ispublic = false; } /** * make requests thingspeak api without additional feed * parameters. * * @param url api url. * @return json string. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ private string thingrequest(string url) throws unirestexception, thingspeakexception { getrequest request = unirest.get(url); if (!this.ispublic) { request.field("key", this.readapikey); } httpresponse<jsonnode> response = request.asjson(); if (response.getcode() != 200) { throw new thingspeakexception("request failed code " + response.getcode()); } return response.getbody().tostring(); } /** * make requests thingspeak api additional feed parameters. * * @param url api url. * @param options optional feed parameters. * @return json string. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ private string thingrequest(string url, feedparameters options) throws unirestexception, thingspeakexception { getrequest request = unirest.get(url); if (!this.ispublic) { request.field("key", this.readapikey); } request.fields(options.fields); httpresponse<jsonnode> response = request.asjson(); if (response.getcode() != 200) { throw new thingspeakexception("request failed code " + response.getcode()); } return response.getbody().tostring(); } /** * use server other thingspeak.com. if hosting own * thingspeak server, set url of server here. url of public * thingspeak server http://api.thingspeak.com * * @param url eg. http://localhost, http://thingspeak.local:3000, etc. */ public void seturl(string url) { this.apiurl = url; } /** * update channel new data. * * @param entry new data posted. * @return id of new entry. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public integer update(entry entry) throws unirestexception, thingspeakexception { httpresponse<string> response = unirest.post(apiurl + "/update") .header(apiheader, this.writeapikey) .header("connection", "close") .fields(entry.getupdatemap()) .asstring(); if (response.getcode() != 200) { throw new thingspeakexception("request failed code " + response.getcode()); } else if (response.getbody().equals("0")) { throw new thingspeakexception("update failed."); } return integer.parseint(response.getbody()); } /** * channel feed default feed options. not include location or status info. fields * have been named in channel's settings (via web) returned. * * @return feed channel. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public feed getchannelfeed() throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/feed.json"; return gson.fromjson(thingrequest(url), feed.class); } /** * channel feed additional feed options. fields have been named in * channel's settings (via web) returned. * * @param options additional feed parameters. * @return feed channel. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public feed getchannelfeed(feedparameters options) throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/feed.json"; return gson.fromjson(thingrequest(url, options), feed.class); } /** * last entry in channel default feed options. * faster alternative getting channel feed , calling * {@link feed#getchannellastentry()}. * * @return entry. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public entry getlastchannelentry() throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/feed/last.json"; return gson.fromjson(thingrequest(url), entry.class); } /** * last entry in channel additional feed options. * faster alternative getting channel feed , calling * {@link feed#getchannellastentry()} * * @param options * @return entry. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public entry getlastchannelentry(feedparameters options) throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/feed/last.json"; return gson.fromjson(thingrequest(url, options), entry.class); } /** * field feed default feed options. * * @param fieldid field include in field (1-8). * @return feed. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public feed getfieldfeed(integer fieldid) throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/field/" + fieldid + ".json"; return gson.fromjson(thingrequest(url), feed.class); } /** * field feed additional feed options. * * @param fieldid field include in field (1-8). * @param options optional parameters control format of feed. * @return feed. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public feed getfieldfeed(integer fieldid, feedparameters options) throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/field/" + fieldid + ".json"; return gson.fromjson(thingrequest(url, options), feed.class); } /** * last entry in field feed default feed options. * * @param fieldid field return (0-8). * @return last entry specified field. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public entry getlastfieldentry(integer fieldid) throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/field/" + fieldid + "/last.json"; return gson.fromjson(thingrequest(url), entry.class); } /** * last entry in field feed additional feed options. * * @param fieldid field return (0-8). * @param options supported options: offset, status, , location. * @return last entry specified field. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public entry getlastfieldentry(integer fieldid, feedparameters options) throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/field/" + fieldid + "/last.json"; return gson.fromjson(thingrequest(url, options), entry.class); } /** * channel status updates. uses default feed options. * * @return status feed. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public feed getstatusfeed() throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/status.json"; return gson.fromjson(thingrequest(url), feed.class); } /** * channel status updates. * * @param options {@link feedparameters#offset(java.lang.integer)} * supported. * @return status feed. * @throws unirestexception request cannot made. * @throws thingspeakexception request invalid. */ public feed getstatusfeed(feedparameters options) throws unirestexception, thingspeakexception { string url = apiurl + "/channels/" + this.channelid + "/status.json"; return gson.fromjson(thingrequest(url, options), feed.class); }
}
Comments
Post a Comment