I have this code in java and I'm trying to port it to C#, I've already ported the majority of the code but I can't really understand how to make this two lines work in C#. I think it's a problem about how the Java code has been wrote because I have nestled two methods in Java and from what I understood it's kind of tricky to do that in C#.
If somebody could help me I would be very happy.
Sorry for my English.
Java code:
package com.example.jsontablewebapi2;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity
{
private Context context;
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
gridView = (GridView) findViewById(R.id.products);
downloadJSON("http://192.168.1.11/api/api.php");
//downloadJSON("http://192.168.1.11:49958");
}
private void createGridView(String json) throws JSONException
{
JSONArray jsonArray = new JSONArray(json);
GridAdapter productAdapter = new GridAdapter( this, jsonArray);
gridView.setAdapter( productAdapter );
}
//I think the problems starts here
private void downloadJSON(final String urlWebService)
{
class DownloadJSON extends AsyncTask<Void, Void, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
try
{
createGridView(s);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids)
{
try {
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null)
{
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e)
{
return null;
}
}
}
DownloadJSON downloadJSON = new DownloadJSON();
downloadJSON.execute();
}
}
C# code:
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;
using Java.IO;
using Java.Net;
using Org.Json;
using System;
using System.Text;
namespace AdrianAndroid
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
private Context context;
private GridView gridView;
protected void onCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
context = this;
gridView = (GridView)FindViewById(Resource.Id.products);
downloadJSON("http://192.168.1.11:49958");
}
public void createGridView(string json)
{
JSONArray jsonArray = new JSONArray(json);
GridAdapter productAdapter = new GridAdapter(this, jsonArray);
gridView.SetAdapter(productAdapter);
}
public class DownloadJSON : AsyncTask<string, string, string>
{
protected override void OnPreExecute()
{
base.OnPreExecute();
}
protected override void OnPostExecute(string s)
{
base.OnPostExecute(s);
try
{
//Error CS0120 on this line
createGridView(s);
}
catch (JSONException e)
{
e.PrintStackTrace();
}
}
protected override string RunInBackground(params string[] @params)
{
try
{
//Error CS0103 on this line (I think because it doesn't know where to get urlWebService
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection)url.OpenConnection();
StringBuilder sb = new StringBuilder();
//Fix1
InputStreamReader inputStream = new InputStreamReader(con.InputStream);
BufferedReader bufferedReader = new BufferedReader(inputStream);
string json;
while ((json = bufferedReader.ReadLine()) != null)
{
sb.Append(json + "\n");
}
return sb.ToString().Trim();
}
catch (Exception e)
{
return null;
}
}
}
public void downloadJSON(string urlWebService)
{
DownloadJSON downloadJSON = new DownloadJSON();
downloadJSON.Execute();
}
}
}