android Json parser offline - android

I'm new to the android world and i have some problem.
I'm developing a project under android and it require a json parser. I get my json file from a web service developed under Zend framework, the link to the web service : "manganew:8080/wsmanganew/manga/manga/idmanga/1" and the content of the json file is
{
"manga": [
{
"idmanga":"1",
"titre":"naruto",
"episode":"145",
"url":"http:\/\/naruto.com\/",
"image":null,
"description":"Naruto Shippuuden .",
"tv":"TV Tokyo",
"dtdebut":"2013-05-23 12:30:00",
"iduser":"1"
}
]}
I'm following this tutorial "http://www.androidhive.info/2012/01/android-json-parsing-tutorial/".
i don't know how index to the web service link in the android, any help will be useful.
thank you

so, what do you need to do? download json object from that url into your app?
that code has method getJSONFromUrl in it's class, you should use it
though, according to comments it has flaws in it.
to read string from file use this code
BufferedReader reader = new BufferedReader(new FileReader("/mnt/sdcard/docs/file.json"));
String line, results = "";
while( (line = reader.readLine()) != null)
results += line;
reader.close();
JSONObject obj = new JSONObject(results);
replace path with path to your file

try this
BufferedReader reader = new BufferedReader(new FileReader("C:\\file.json"));
String line, results = "";
while( (line = reader.readLine()) != null)
results += line;
reader.close();
JSONObject obj = new JSONObject(results);
Replace with your local machine download field path C:\\file.json

Related

Xamarin exec logcat command and read results to string

Is it possible to exec commands in a Xamarin application? Consider the following:
var process = Java.Lang.Runtime.GetRuntime().Exec("logcat");
var hasExited= await process.WaitForAsync();
I would like to be able to take the results of process and read it to a string. This can be done in native Android, but I am looking for a Xamarin C# solution. Any help is greatly appreciated.
Xamarin.Android is based on native Android.So you can call this method in Xamarin.Android .
var process = Java.Lang.Runtime.GetRuntime().Exec("getprop");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.InputStream));
StringBuilder builder = new StringBuilder();
string line;
while ((line = bufferedReader.ReadLine()) != null)
{
builder.Append(line + "\n");
}
System.Console.WriteLine(builder.ToString());

Parsing JSON remote call and store it locally

I need to parse a remote response from a URL that retrieves a big JSON object and store it locally so we can perform searches for example, in an activity in Android.
I've been trying to look it up but I can't find any approach about how to do it.
Any suggestion/help please?
Thanks a lot in advance!
You can use Gson parsing to parse the JSON from url.
For Storing the result use sqlite Database.
Here is the good example for GSON parsing
For sqlite follow this link
the retrieved JSON response is stored in the BufferReader i.e.,
urlClientStream = openHttpClientPostConnection(searchURL,
urlParams.toString());
BufferedReader reader = new BufferedReader(
new InputStreamReader(urlClientStream), 8192);
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
JSONObject mainObj = new JSONObject(buffer.toString())
.getJSONObject("RESPONSE");
Hope this helps you

JSON Parsing without using URL to read json file in android

I am using url to take the file for parsing. Now i want read the file which is in sdcard for parsing.How can i do this?
here my code
reading file from url
private static String url = "http://10.0.2.2/device1.json";
JSONObject json = jParser.getJSONFromUrl(url);
devices = json.getJSONArray(TAG_DEVICES);
now i want do this from reading the file in sdcard
java.io.File device = new java.io.File("/mnt/sdcard/device1.json");
FileReader device_Fr = new FileReader(device);
next how to pass this file for parsing?
Use this code
read data from file into string & pass that to JSONObject.
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line, results = "";
while( ( line = reader.readLine() ) != null)
{
results += line;
}
reader.close();
JSONObject obj = new JSONObject(results);
One way to do it is to read the json and store it in a string. Then parse it like this:
String jsonStr = // read from file
JSONObject json = new JSONObject( jsonStr);
// parse
 
  devices = json.getJSONArray(TAG_DEVICES);

How to get data from html in android

I have get html data from webpage. But i want to get only data excluding html tags.
I have tried this:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(urlText.getText().toString());
// Get the response
BufferedReader rd = new BufferedReader(new InutStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null)
{
textView.append(line);
sb.append(line+"\n");
}
This giving me whole html data. Tell me now i can get data only.
Have you tried using Html.fromHtml(source)? or use any Java HTML parser (If they work on android) for this.
Here source is your html formatted whole data.
EDIT:
while ((line = rd.readLine()) != null)
{
sb.append(line+"\n");
}
String source = sb.toString();
textView.setText(Html.fromHtml(source));
Look at this example Android Parsing HTML Content Containing Links.

Making a web request and getting request in JSON format in android

I am trying to accomplish the task of
Making a Web request
->getting result in JSON format
->Parsing the result
-> and finally display the result in a table....
Any help regarding any of the task is welcome....
I'll leave you to display the results, but the first bit can be accomplished as follows:
URLConnection connection = new URL("http://example.com/someService.jsp").openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 1024 * 16);
StringBuffer builder = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
JSONObject object = new JSONObject(builder.toString());

Categories

Resources